Sei sulla pagina 1di 24

C PROGRAMMING LANGUAGE

1. What is C function?
A large C program is divided into basic building blocks called C function. C
function contains set of instructions enclosed by “{ }” which performs specific
operation in a C program. Collection of these functions creates a C program.

2. Uses of C functions:

 C functions are used to avoid rewriting same logic/code again and


again in a program.
 There is no limit in calling C functions to make use of same functionality
wherever required.
 We can call functions any number of times in a program and from any
place in a program.
 A large C program can easily be tracked when it is divided into
functions.
 The core concept of C functions are, re-usability, dividing a big task
into small pieces to achieve the functionality and to improve
understandability of very large C programs.

3. C function declaration, function call and function definition:


There are 3 aspects in each C function. They are
 Function declaration or prototype – This informs compiler about the
function name, function parameters and return value’s data type.
 Function call – This calls the actual function
 Function definition – This contains all the statements to be executed.

C functions aspects syntax


Return type function name
(arguments list)
function definition { Body of function; }
function name (arguments
function call list);
return type function name
function declaration (argument list);

SIMPLE EXAMPLE PROGRAM FOR C FUNCTION:

 In the below program, function “square” is called from main function.


The value of “m” is passed as argument to the function “square”. This
value is multiplied by itself in this function and multiplied value “p” is
returned to main function from function “square”.

#include<stdio.h>
float square ( float x );

main( )
{
float m, n ;
printf ( "\nEnter some number for finding square ");
scanf ( "%f", &m ) ;
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}

float square ( float x )


{
float p ;
p=x*x;
return ( p ) ;
}

OUTPUT:
Enter some number for finding square
2
Square of the given number 2.0 is 4.0

4. HOW TO CALL C FUNCTIONS IN A PROGRAM?


There are two ways that a C function can be called from a program. They
are,

1. Call by value
2. Call by reference
1. CALL BY VALUE:
 In call by value method, the value of the variable is passed to the function
as parameter.
 The value of the actual parameter cannot be modified by formal
parameter.
 Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.

Note:

 Actual parameter – This is the argument which is used in function call.


 Formal parameter – This is the argument which is used in function
definition
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY VALUE):
 In this program, the values of the variables “m” and “n” are passed to the
function “swap”.
 These values are copied to formal parameters “a” and “b” in swap
function and used.
#include<stdio.h>
void swap(int a, int b);

main()
{
int m = 22, n = 44;

printf(" values before swap m = %d \nand n = %d",


m, n);
swap(m, n);
}

void swap(int a, int b)


{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d",
a, b);
}

OUTPUT:
values before swap m = 22
and n = 44
values after swap m = 44
and n = 22

2. CALL BY REFERENCE:
 In call by reference method, the address of the variable is passed to the
function as parameter.
 The value of the actual parameter can be modified by formal parameter.
 Same memory is used for both actual and formal parameters since only
address is used by both parameters.
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY
REFERENCE):
 In this program, the address of the variables “m” and “n” are passed to
the function “swap”.
 These values are not copied to formal parameters “a” and “b” in swap
function.
 Because, they are just holding the address of those variables.
 This address is used to access and change the values of the variables.

#include<stdio.h>
void swap(int *a, int *b);

main()
{ int m = 22, n = 44;

printf("values before swap m = %d \n and n =


%d",m,n);
swap(&m, &n);
}

void swap(int *a, int *b)


{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \nand b = %d", *a,
*b);
}
OUTPUT:
values before swap m = 22
and n = 44
values after swap a = 44
and b = 22

Operators in C

Arithmetic Operators:

C Arithmetic operators are used to perform mathematical calculations like addition,


subtraction, multiplication, division and modulus in C programs.

Arithmetic Operators/Operation Example


+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B
Example:
#include <stdio.h>

int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}

PROGRAMS
1.Program to display Hello World !

#include<stdio.h>
main()
{
printf(“Hello World!”);
getch()
return;
}
2.Write the program to input an integer and display the value
#include<stdio.h>
main()
{
int a;
printf(“Enter the number”);
scanf(“%d”,&a);
printf(“the number you entered is : %d \n”,a);
getch();
return;
}
3.Write a program to declare one integer variable,character and floating point number &
display all the values on the screen.
#include<stdio.h>
main()
{
int a=20;
char ch=’a’;
float f=19.7675;
printf(“The number you entered is %d \n”,a);
printf(“The character you entered is %c \n”,ch);
printf(“The floating point number is %f \n”,f);
getch();
return;
}
4.Write a program to do all arithmetic operations of two numbers.

#include <stdio.h>
#include<conio.h>

int main()
{
clrscr();
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf ("Addition of a, b is : %d\n", add);
printf ("Subtraction of a, b is : %d\n", sub);
printf ("Multiplication of a, b is : %d\n", mul);
printf ("Division of a, b is : %d\n", div);
printf ("Modulus of a, b is : %d\n", mod);
getch();
return;
}
5.Write a program to input two integer numbers and find the sum
#include <stdio.h>
#include<conio.h>

int main()
{
clrscr();
Int a,b,add;
printf(“enter the first number”);
scanf(“%d”,&a);
printf(“enter the second number”);
scanf(“%d”,&b);

add = a+b;
printf ("Addition of a, b is : %d\n", add);
getch();
return;
}

6. Program to find the area of the rectangle


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int len,br;
printf(“Enter the length”);
scanf(“%d”,&len);
printf(“Enter the breadth”);
scanf(“%d”,&br);
printf(“The area is %d”,len*br);
getch();
return;
}
7. Write the program to find the sum of two numbers using function.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int sum;
sum=add();
printf(“The sum is %d”,sum);
getch();
return 0;
}
Int add()
{
Int a,b,c;
printf(“enter the first number”);
scanf(“%d”,&a);
printf(“enter the second number”);
scanf(“%d”,&b);

c = a+b;
return c;
}
8. write the program to find the square of a no using function

#include<stdio.h>
#include<conio.h>
main()
{
int a,s;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&a);
s=sqr(a);
printf(“The square root is :%d”,s);
getch();
return;
}
Int sqr (int x)
{
int c=x*x;
return c;
}
9.write the program to find the product of two numbers

#include <stdio.h>

#include<conio.h>
int main()
{
int x,y,z;
clrscr();

printf( "Please input two numbers to be multiplied: "


);
scanf( "%d", &x );
scanf( "%d", &y );
z=mult(x,y);
printf( "The product of your two numbers is %d\n",z);
getch();
}

int mult (int x, int y)


{
return x * y;
}

If Statement
The simplest if structure involves a single executable statement.
Execution of the statement occurs only if the condition is true.
Syntax:
if (condition)
statement;
Example:
#include<stdio.h>
#include<conio.h>
main ()
{
int marks;
clrscr();

printf("Enter your marks:");


scanf("%d",&marks);
if(marks >=50)
printf("CONGRATULATIONS...!!! you have passed.");
getch();
return 0;
}
Limitation of If
The statement(s) are executed if the condition is true; if the
condition is false nothing happens in other words we may say it
is not the effective one. Instead of it we use if-else statements
etc.

If-else statement
In if-else statement if the condition is true, then the true
statement(s), immediately following the if-statement are
executed otherwise the false statement(s) are executed. The use
of else basically allows an alternative set of statements to be
executed if the condition is false.
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}

Example:
#include<stdio.h>
#include<conio.h>
main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else
printf("%d is not a leap year.",y);
getch();
return 0;
}

IF -else if statement
It can be used to choose one block of statements from many
blocks of statements. The condition which is true only its block
of statements is executed and remaining are skipped.
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}

Example:
#include<stdio.h>
#include<conio.h>
main ()
{
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
printf("The number is positive.");
else if (n<0)
printf("The number is negetive.");
else
printf("The number is zero.");
getch();
return 0;
}
Nested IF
In nested-if statement if the first if condition is true the control
will enter inner if. If this is true the statement will execute
otherwise control will come out of the inner if and the else
statement will be executed.
Syntax:
If (condition)
if(condition)
{
statement(s);
}
else
{
statement(s);
}
else
{
statement(s);
}
Example:

Program to relate two integers using =, > or <

#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}

else
{
printf("Result: %d < %d",number1, number2);
}

return 0;

Switch Statement
Switch statement is alternative of nested if-else.it is executed
when there are many choices and only one is to be executed.

Syntax:
switch(expression)
{
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;
default:
statement;
}

Example:
#include<stdio.h>
#include<conio.h>
main ()
{
char c;
clrscr();
printf("Enter an alphabet:");
scanf("%c",&c);
switch(c)
{
case'a':
case'A':
printf("You entered vowel.");
break;
case'e':
case'E':
printf("You You entered vowel.");
break;
case'i':
case'I':
printf("You entered vowel.");
break;
case'o':
case'O':
printf("You entered vowel.");
break;
case'u':
case:'U'
printf("You entered vowel.");
break;
default:
printf("You entered a consonant.");
}
getch();
Return;
}
LOOPS :
Loops are used in programming to repeat a specific block until
some end condition is met. There are three loops in C
programming:
1. for loop
2. while loop
3. do...while loop
for Loop
The syntax of for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// codes
}

How for loop works?


The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is
false (0), for loop is terminated. But if the test expression is true
(nonzero), codes inside the body of for loop is executed and
the update expression is updated.
This process repeats until the test expression is false.
The for loop is commonly used when the number of iterations is
known.
Example of For loop

#include <stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
getch();
return 0;
}

Example 2: Sum of Natural Numbers Using for Loop

The positive numbers 1, 2, 3... are known as natural


numbers. The programs below takes a positive integer (let
say n) as an input from the user and calculates the sum up
to n.

#include<stdio.h>
#include<conio.h>
main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);

for(i=1; i <= n; ++i)


{
sum += i;
}

printf("Sum = %d",sum);
getch();
return 0;
}

Example 3: Multiplication Table Up to 10


The program takes an integer input from the user and generates the
multiplication table up to 10.

#include<stdio.h>
#include<conio.h>
main()
{
int n, i;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);

for(i=1; i<=10; ++i)


{
printf("%d * %d = %d \n", n, i, n*i);
}
getch();
return 0;
}

While Loop
The most basic loop in C is the while loop and it is used is to
repeat a block of code. A while loop has one control expression
(a specific condition) and executes as long as the given
expression is true.

Syntax
while (condition)
{
statement(s);
}

Example of while loop


#include <stdio.h>
#include<conio.h>
main()
{
int count=1;
clrscr();
while (count <= 4)
{
printf("%d ", count);
count++;
}
getch();
return 0;
}

step1: The variable count is initialized with value 1 and then it


has been tested for the condition.

step2: If the condition returns true then the statements inside the
body of while loop are executed else control comes out of the
loop.
step3: The value of count is incremented using ++ operator then
it has been tested again for the loop condition.

2)C program to read an integer and print its


multiplication table
#include<stdio.h>
#include<conio.h>
main()
{
int num,i;

clrscr();
printf("Enter an integer number: ");
scanf("%d",&num);
i=1;

while(i<=10)
{
printf("%d\n",(num*i));
i++;
}
getch();
return 0;
}
Do While Loop:

Potrebbero piacerti anche