Chapter 2
List and explain various conditional statements in C
Various Conditional statements available in C are:
1. Simple if statement
2. if –else statement
3. else if ladder or if-else-if statement
4. Nested if else statement
Simple if statement:
The Syntax of if statement in C :
if ( condition)
//code
How it works:
The if statement checks the condition inside the paranthesis( )
If the condition is evaluated to true, statements inside the body of if are
executed
Fig: Flow chart of Simple if statement
Example program for simple if statement:
// Program to display a number if it is negative
#include<stdio.h>
#include<conio.h>
void main( )
int num;
clrscr();
printf(“\n Enter a number “);
scanf(“%d”, &num);
if(num< 0)
printf(“\n You Entered : %d”, num);
printf(“\n Simple if statement is Easy to learn”);
if-else statement:
The Syntax of if –else statement is:
if(condition)
//run code if condition is true
else
//run code if condition is false
How if....else statement works?
If the condition is evaluated to true,
Statements inside the body of if are executed
Statements inside the body of else are skipped from execution.
If the condition is evaluated to false,
Statements inside the body of else are executed
Statements inside the body of if are skipped from execution.
Flow Chart of if – else statement:
Example: if –else statement
// Program to check if a given number is even or odd
#include<stdio.h>
#include<conio.h>
void main()
int num;
clrscr();
printf(“\n Enter a number”);
scanf(“%d”, &num);
if(num % 2 ==0)
printf(“ \nEntered number %d is an even number”, num);
else
printf(“\n Entered number %d is an odd number”, num);
if....else Ladder:
The if...else statement executes two different codes depending upon whether the
test condition is true or false. Sometimes, a choice has to be made from more than 2
possibilities.
The if ...else ladder allows you to check multiple conditions and execute different
statements
Syntax of if..else Ladder:
if(condition 1) {
//statements
else if (condition2) {
//statements
}
else if(condition3){
//statements
else {
//statements
Flow chart of if-else ladder:
Example 1: if...else ladder
// Program to find biggest among two numbers
#include<stdio.h>
#include<conio.h>
void main( )
int a,b,c;
clrscr();
printf(“\n Enter three numbers : ”);
scanf(“%d %d %d”, &a, &b, &c);
if(a>b && a>c)
printf(“\n %d is bigger”,a);
else if(b>a && b>c)
printf(“\n %d is bigger”, b);
else
printf(“\n %d is bigger”, c);
}
getch();
output:
Enter three numbers : 10 30 20
30 is bigger
Example 2: if-else ladder
//Program to relate two integers using <, = or > symbols
#include<stdio.h>
#include<conio.h>
void main( )
int num1, num2;
clrscr( );
printf(“\n Enter two numbers”);
scanf(“%d %d”, &num1, &num2);
if( num1 > num2)
printf(“\n Result : %d > %d”, num1, num2);
else if(num1 < num2)
{
printf(“ \n Result: %d < %d”, num1, num2);
else
printf(“\n Result: %d = %d”, num1, num2);
getch( );
Nested if...else:
It is possible to include if...else statement inside body of another if..else statement.
Syntax:
if( condition1 )
{
if( condition2 )
{
//code to execute if condition 1 and condition 2 are true
}
else
{
//code to execute if condition 1 is true and condition 2 is false
}
}
else
{
if( condition3 )
{
//code to execute if condition 1 is false and condition 3 is true
}
else
{
//code to execute if condition 1 is false and condition 3 is false
}
}
Example for Nested if .... else statement:
// Program to check if a given year is leap year or not
#include<stdio.h>
#include<conio.h>
void main( )
int year;
clrscr();
printf(“\n Enter a year to check leap year or not”);
scanf(“%d”, &year);
if(year %100! =0)
{
if(year %4 == 0)
{
printf(“\n Given year is a Leap Year “);
}
else
{
printf(“\n Given year is not a Leap year”);
}
}
else
{
if(year % 400 ==0)
{
printf(“\n Given year is a Leap Year”);
}
else
{
printf(“\n Given year is not a Leap Year”);
}
}
getch();
}
Explain about switch statement in C?
This is a multi-way conditional control statement which helps us to make choice
among a number of alternatives.
The Syntax of switch statement is:
switch (expression)
{
case constant1:
statement
....................
case constant2:
statement
.....................
........................
.........................
........................
case constantN:
Statement
...................
default:
Statement
.....................
}
Here switch, case and default are keywords.
How switch statement works:
Firstly the switch control expression is evaluated, if the value of the expression
matches with any case constant expression, then the control is transferred to that
case label. If none of the case constant matches with the value of the expression
then the control is transferred to the default label. The label default is optional.
Example Program on switch case:
// Program to print week days using switch case
#include<stdio.h>
#include<conio.h>
void main( )
{
int day;
clrscr( );
printf(“\n Enter a number between 1 to 7: “);
scanf(“%d”, &day);
switch(day)
{
case 1: printf(“\n Sunday”);
break;
case 2: printf(“\n Monday”);
break;
case 3: printf(“\n Tuesday”);
break;
case 4: printf(“\n Wednesday”);
break;
case 5: printf(“\n Thursday”);
break;
case 6: printf(“\n Friday”);
break;
case 7: printf(“\n Saturday”);
break;
default: printf(“\n Please enter number between 1 to 7”);
getch();
}
List the different iterative loops and explain them ( for, do, while statements)
Loops/Iterative statements help you do the same thing again and again without
writing the same code over and over.
Advantages of using Loop statements are:
1. Repetition: Loop statements allow you to repeat a block of code multiple times,
which is essential for performing repetitive tasks efficiently.
2. They help in writing efficient code by avoiding duplication of code
3. Loop statements make your code more concise and easier to read.
Iterative/ Loop Statements Available in C are:
1. while loop
[Link] while loop
[Link] loop
while loop:
Syntax:
while (condition)
{
body of while loop
}
How while loop works:
First the condition is checked, if it is true then the statements inside the body of loop
are executed. After the execution, again the condition is checked and if it is true,
again the statements in the body of loop are executed. This means that these
statements are executed continuously till the condition is true and when it becomes
false, the loop terminates and the control come out of the loop.
Flow Chart:
Example of while loop:
// Program to print natural number from 1 to 10
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
clrscr( );
i=1;
while(i<=10)
{
printf(“%d\t”, i);
i=i+1;
}
getch( );
}
Output: 1 2 3 4 5 6 7 8 9 10
// Program to print even numbers from 2 to 100
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
clrscr( );
i = 2;
while( i< =100)
{
printf(“%d\t”, i);
i=i+2;
getch( );
}
Output: 2 4 6 8 10 12 14 16 18 ...................100
do while loop:
Syntax:
do
{
body of do while loop
} while( condition);
Flow Chart of do-while loop:
How do while loop works:
Here first the loop body is executed and then the condition is checked. If the
condition is true, then again the loop body is executed and this process continues as
long as the condition is false. When the condition becomes false the loop
terminates.
Example of do-while loop:
// Program to print numbers from 1 to 10 using do-while loop
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
clrscr();
i=1;
do
{
printf(“%d\t”, i);
i= i + 1;
} while(i<=10);
getch();
}
// Program to print odd numbers between 1 to 99 using do-while loop
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
clrscr();
i=1;
do
{
printf(“%d\t”, i);
i= i + 2;
} while(i<100);
getch();
}
Output: 1 3 5 7 9 11 13 15 17 19 21 ....................99
for loop:
Syntax:
for ( initialization expression ; condition ; update expression
)
{
Body of for loop
}
Flow Chart of for loop:
How for loop works:
Firstly, the initialization expression is executed and the loop variables are
initialized, and then the condition is checked, if the condition is true then the body
of loop is executed. After executing the loop body, control transfers to update
expression and it modifies the loop variable and then again the condition is checked,
and if it is true, the body of loop is executed. This process continues till the condition
is true and when the condition becomes false the loop is stopped and the control is
transferred to the next statement after the loop.
Example of for loop:
// Program to print numbers from 1 to 10 using for loop
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf(“ %d\t”, i);
}
getch();
// Program to find the sum of the series 1 + 2 + 3 + ..................n
#include<stdio.h>
#include<conio.h>
void main( )
{
int i, sum=0,n;
clrscr();
printf(“\n Enter number of terms”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
sum = sum + i;
}
printf(“\n Sum of first %d terms = %d”, n, sum);
getch();
}
Output:
Enter number of terms 5
sum of first 5 terms = 15
Sample Programs on Loops Concepts
// Program to check if a given number is Palindrome or not
#include<stdio.h>
#include<conio.h>
void main( )
int num, temp, rev=0;
clrscr();
printf(“\n Enter a number”);
scanf(“%d”, &num);
temp=num;
while(num ! =0)
rev= rev* 10 + (num%10);
num=num/10;
if(temp==rev)
printf(“\n Given number is a palindrome “);
else
printf(“\n Given number is not a Palindrome”);
// Program to check if a given number is Armstrong number or not
#include<stdio.h>
#include<conio.h>
void main()
int num, arm=0, temp, rem;
clrscr();
printf(“\n Enter a number to check Armstrong number or not”);
scanf(“%d”, &num);
temp=num;
while( num! =0)
rem = num%10;
arm= arm+(rem*rem*rem);
num=num/10;
if( arm == temp)
printf(“ \n Given number is an Armstrong number”);
else
printf(“\n Given number is not an Armstrong number”);
// Program to generate Fibonacci Series up to n terms
#include<stdio.h>
#include<conio.h>
void main( )
int n1=0,n2=1, n3, n,i;
clrscr();
printf(“\n Enter number of terms to generate Fibonacci Series”);
scanf(“%d”, &n);
printf(“\n Fibonacci Series”);
printf(“%d\t%d\t”,n1,n2);
i=2;
while(i<n)
n3=n1+n2;
printf(“%d\”, n3);
n1=n2;
n2=n3;
i++;
}
Chapter 3
Define Array?
It is a collection of elements of same data type. Elements of an array are stored in
Contiguous memory locations.
(Or)
Collection of Homogenous elements.
Example:
int arr[5]={10,20,30,40,50};
10 20 30 40 50
arr[0] arr[1] arr[2] arr[3] arr[4]
Types of Arrays:
1D Array: An array with one subscript is known as One Dimensional Array( 1D Array)
2D Array: An array with two subscripts is known as Two Dimensional Array(2D Array)
Declaration and Initialization of 1-D Array:
Syntax: Declaration of 1-D Array
datatype array_name [ size ];
Ex:
1. int arr[20];
Here ‘arr’ is the name of the array and 20 is the size of the array.
2. float marks[15];
Here ‘marks’ is the name of the array and 15 is the size of the array.
Initialization of 1-D Array:
datatype array_name[size] = { v1, v2,v3, .........};
Ex: int arr[5]= {10,20,30,40,50};
float b[5];
b[0]=1.0;
b[1]=2.0;
b[2]=3.0;
b[3]=4.0;
b[4]=5.0;
Declaration and Initialization of 2-D Array:
Syntax: Declaration of 2-D Array
datatype array_name [row][col];
Here “row” represents number of rows and “col” represents number of columns
Ex:
int arr[3][3];
Here ‘arr’ is 2D array with 3 rows and 3 columns. Total of 3*3 = 9 elements are
present in the 2D array.
float b[3][4];
Here ‘b’ is 2D array with 3 rows and 4 columns. Total of 3*4=12 elements are present
in the 2D array.
Initialization of 2D Array:
int arr[2][2] = {10,20,30,40};
10 20 30 40
arr[0][0] arr[0][1] arr[1][0] arr[1][1]
Fig: Memory allocation in 2D Array
Programs on 1D Arrays:
Write a C Program to create a 1 D Array and display elements of it
#include<stdio.h>
#include<conio.h>
void main( )
{
int arr[25], i, n;
clrscr();
printf(“\n Enter the size of 1D Array”);
scanf(“%d”,&n);
printf(“\n Enter %d elements “, n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“\n Elements of the array are :\t”);
for(i=0;i<n;i++)
printf(“%d\t”, a[i]);
getch();
}
Write a C Program to sort ‘n’ numbers in Ascending Order
#include<stdio.h>
#include<conio.h>
void main( )
int a[50],i,j,n,temp;
clrscr();
printf(“\n Enter the size of an array”);
scanf(“%d”, &n);
printf(“\n Enter %d elements”,n);
for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
printf(“\n Elements before sorting are: “);
for(i=0;i<n;i++)
printf(“%d\t”, a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
getch( );
Write a C Program to add two 3 X 3 matrices
#include<stdio.h>
#include<conio.h>
void main( )
int a[3][3], b[3][3], c[3][3],i,j;
clrscr();
printf(“Enter 9 elements for Matrix A \n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”, &a[i][j]);
printf(“\n Enter 9 elements for Matrix B\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”, &b[i][j]);
printf(“\n Elements of Matrix A are \n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
printf(“\n Elements of Matrix B are\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,b[i][j]);
printf(“\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j] + b[i][j];
printf(“\n Result Matrix C is\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,c[i][j]);
printf(“\n”);
getch();
Write a C Program to subtract 3 X 3 matrices
#include<stdio.h>
#include<conio.h>
void main( )
int a[3][3], b[3][3], c[3][3],i,j;
clrscr();
printf(“Enter 9 elements for Matrix A \n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
printf(“\n Enter 9 elements for Matrix B\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”, &b[i][j]);
printf(“\n Elements of Matrix A are \n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,a[i][j]);
printf(“\n”);
}
printf(“\n Elements of Matrix B are\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,b[i][j]);
printf(“\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j] - b[i][j];
printf(“\n Result Matrix C is\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf(“%d\t”,c[i][j]);
printf(“\n”);
getch();
Write a C Program to multiply two 3 X 3 matrices
#include<stdio.h>
#include<conio.h>
void main( )
int a[3][3], b[3][3], c[3][3],i,j;
clrscr();
printf(“Enter 9 elements for Matrix A \n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”, &a[i][j]);
}
printf(“\n Enter 9 elements for Matrix B\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(“%d”, &b[i][j]);
printf(“\n Elements of Matrix A are \n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,a[i][j]);
printf(“\n”);
printf(“\n Elements of Matrix B are\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf(“%d\t”,b[i][j]);
printf(“\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j] = a[i][j] + b[i][j];
printf(“\n Result Matrix C is\n”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
printf(“%d\t”,c[i][j]);
printf(“\n”);
getch();
}
Write a C Program to find transpose a given m X n matrix
#include<stdio.h>
#include<conio.h>
void main() {
int matrix[10][10], i, j, rows, cols, transpose[10][10];
clrscr();
printf(“Enter number of rows for matrix \n”);
scanf(“%d”, &rows);
printf(“Enter number of columns for matrix\n”);
scanf(“%d”, &cols);
printf(“\n Enter %d elements of the matrix : “, (rows*columns));
for(i=0;i<rows;i++)
{ for(j=0;j<cols;j++)
scanf(“%d”, &matrix[i][j]);
printf(“ Original Matrix :\n”);
for(i=0;i<rows;i++)
{ for(j=0;j<cols;j++)
printf(“%d\t”,matrix[i][j]);
}
}
//calculate the transpose of the matrix
for(i=0;i<cols;i++)
for(j=0;j<rows;j++)
transpose[i][j] = matrix[j][i];
Strings:
A String is a Collection of Characters enclosed with in double quotes. Each string is
terminated with a Null character (\0).
(or)
An array of characters is known as a String.
Ex:
char str[20]=”Polytechnic”;
P o l y t e c h n i c ‘\0’
str[0] str[1] str[2] str[3] str[4] str[5] str[6] str[7] str[8] str[9] str[10] str[11]
Fig: Storage of string “Polytechnic” in memory
Declaration of Strings:
char array_name[size];
Here array_name is the name of the your character array(String) and “size” is the size
or length of the array.
Example:
char str[10];
Here ‘str’ is the name of the character array(string) and size of the string is 10.
Initialization of Strings:
char str[10]=”Hello”;
(or)
char str[ ] = “Hello”;
Here size of the string is Optional, as we are initializing the string at the declaration
time.
Functions used for Reading Strings:
Function Description Example
scanf() Reads formatted input char name[50];
including strings. scanf(“%s”,name);
Format specifier %s is
used to read a string.
gets( ) Used to read a line of char name[50];
text from the standard gets(name);
input.
fgets( ) Read a line of text from char name[50];
specified file stream(eg: fgets(name,sizeof(name),stdin);
‘stdin’ for keyboard
input)
Functions used for Writing Strings:
Function Description Example
printf() Used to print output to char message[50]=”Hello
the standard World”;
output(console). printf(“%s\n”,message);
puts( ) Prints a string followed char message[50]=”Hello
by a new line World”;
character(‘\n’) to the puts(message);
standard output.
fputs( ) Writes a String to a char message[50]=”Hello
specified file stream(eg. World”;
‘stdout’ for monitor fputs(message,stdout);
output)
String Handling Functions in C:
Function Description Example
strlen(str) Calculates the length of char str[]=”Hello”;
the given string int len=strlen(str);
printf(“%d”,len);
strcpy(dest,src) Copies the content of one char dest[20];
string(‘src’) to char src[ ] =”Hello World”;
another(‘dest’) strcpy(dest,src);
strcat(dest,src) Concatenate(join) one char str1[20]=”Hello “;
string(‘src’) to the end of char str2[]=”World!”;
another(‘dest’) strcat(str1,str2);// str1 becomes
“Hello World”
strcmp(str1,str2) Compares two char str1[]=”Ramu”;
strings(‘str1’ and ‘str2’) char str2[]=”Ramu”;
and check is both are int value=strcmp(str1,str2);
same or not. If both are Here both strings str1 and str2 are
same, it will return 0, same, so strcmp() function return
otherwise it will return 0, and it is stored in variable ‘value’
either positive or negative
integer number.
strncpy(dest,src,n) Copies a specified char src[ ]=”Hello World”;
number(‘n’) of characters char dest[20];
from one string(‘src’) to strncpy(dest,src,5);
another (‘dest’) Here string ‘dest’ will contain
“Hello” as we are copying first 5
characters only.
strncmp(str1,str2,n) Compared first ‘n’ number char str1[]=”apple”;
of characters from two char str2[]=”appetizer”;
strings(‘str1’ and ‘str2’) int result=strcmp(str1,str2,3);
Here result will have value Zero,
because first three characters are
the same.
Sample Programs on String Handling functions
// Write a C Program to find length of a given string using strlen()
function
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
int len;
char str[25];
clrscr();
puts(“Enter a string”);
gets(str);
len=strlen(str);
printf(“\n Length of given string = %d”, len);
getch();
Output:
Enter a String Tirupati
Length of given string= 8
// Write a C Program to understand strcpy() function
#include<stdio.h>
#include<conio.h>
void main()
char src[25]=”Hello World”, dest[25];
clrscr();
printf(“\n Source String is %s”, src);
strcpy(dest,src);
printf(“\nDestination String is %s”, dest);
getch();
Output:
Source String is Hello World
Destination String is Hello World
// Write a C Program to understand strcpy() function
#include<stdio.h>
#include<conio.h>
void main()
char src[25]=”Hello World”, dest[25];
clrscr();
printf(“\n Source String is %s”, src);
strcpy(dest,src);
printf(“\nDestination String is %s”, dest);
getch();
Output:
Source String is Hello World
Destination String is Hello World
// Write a C Program to understand strcat() function
#include<stdio.h>
#include<conio.h>
void main()
char src[25]=”Hello ”, dest[25]=”World”;
clrscr();
printf(“Before Concatenation:\n”);
printf(“Source String is %s”, src);
printf(“\n Destination String is %s”,dest);
strcat(src,dest);
printf(“\n After Concatenation:”);
printf(“\n Source String is %s”, src);
printf(“\nDestination String is %s”, dest);
getch();
Output: Before Concatenation:
Source String is Hello
Destination String is World
After Concatenation
Source String is Hello World
Destination String is World
// Write a C Program to understand strcmp() function
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[25],str2[25];
clrscr();
printf(“\n Enter string1”);
gets(str1);
printf(“\n Enter string2”);
gets(str2);
if(strcmp(str1,str2)==0)
{
printf(“\n Both Strings are equal”);
}
else
{
printf(“\n Both strings are not equal”);
}
getch();
}
// write a C Program to reverse a given string without using strrev() function
#include<stdio.h>
#include<conio.h>
void main()
char str[100],temp;
int len,i;
clrscr();
printf(“\nEnter a string”);
scanf(“%s”,str);
len=strlen(str);
for(i=0;i<len/2;i++)
temp=str[i];
str[i]=str[length-i-1];
str[length-i-1]=temp;
printf(“\n Reverse String: %s\n”, str);
//Write a C Program to check if a given string is Palindrome or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
char str1[25];
int left,right,flag=1;
clrscr();
puts("Enter a string");
gets(str1);
left=0;
right=strlen(str1)-1;
while(left<right)
if(str1[left]!=str1[right])
flag=0;
break;
left++;
right--;
if(flag==1)
printf("\nGiven string is palindrome");
else
printf("\nGiven string is not a palindrome");
getch();
}
Chapter 4
Functions:
Def: A function is a self-contained block used to perform specific task.
Advantages of functions:
1. Modularity
2. Reusability
3. Easy Maintenance
4. Debugging is easier
Types of functions:
1. Library Functions:
The functions which are present in the C-Library are called Built-in /Library Functions.
Example:
printf( ), scanf( ), strlen( ), strcpy( ) etc
2. User Defined functions:
The functions which are created by the users are called user defined functions.
Ex: sum( ), display_salary( ), get_studentinfo( ) etc
To create and use these functions, we should know about three things, they are:
1. Function declaration
2. Function Definition
3. Function call
Function Declaration:
The function declaration is used to give specific information to the compiler about
the function like what is its return type, how many arguments it is taking, name of
the function.
Syntax:
returntype function_name ( type1 par1, type2 par2, type3 par3, ................);
Example:
int sum( int x, int y);
float display_marks( );
Function Definition:
The function definition consists of the whole description and code of a function. It
tells what the function is doing and what is its input and output.
Syntax:
returntype func_name( parameter declaration)
body of a function
Function call:
The function definition describes what a function can do, but to actually use it in the
program the function should be called.
A function is called by simply writing its name followed by the arguments list inside
the parentheses.
Syntax:
func_name(arg1, arg2, arg3,.....);
Example:
fun(x);
fun(28, 38);
We can classify the functions into four types depending on the return value
and arguments
1. Functions with no arguments and no return value
2. Functions with no arguments but a return value
3. Functions with arguments but no return value
4. Functions with arguments and return value
Functions with arguments but no return value:
In this model, function will not return anything to the calling function, but it will take
arguments.
#include<stdio.h>
#include<conio.h>
void sum(int , int); // Function Prototype
void main( )
int a,b,c;
clrscr( );
printf(“\nEnter 2 numbers”);
scanf(“%d %d”, &a, &b);
sum(a,b); // Function Call
getch( );
void sum(int x, int y) //Function Definition
int z;
z = x+y;
printf(“\n Result = %d”, z);
Functions with arguments and return value:
In this model, function will return value to the calling function, and it also take
arguments.
#include< stdio.h>
#include <conio.h>
int sum( int ,int );
void main( )
int a,b,c;
clrscr( );
printf(“\n Enter 2 numbers”);
scanf(“%d %d”, &a, &b);
c= sum(a,b);
printf(“\n Result = %d”, c);
getch( );
int sum( int x, int y)
return (x+y);
Functions with no arguments but a return value
In this model, function will not take any arguments but return value to the calling
function
#include<stdio.h>
#include<conio.h>
int sum ( );
void main( )
int result;
clrscr( );
result = sum( );
printf(“\n Sum = %d”, result) ;
getch( );
}
int sum( )
int a,b,c;
printf(“\n Enter 2 numbers” );
scanf(“%d %d”, &a, &b);
return (a+b);
Functions with no arguments and no return value:
In this model, the function will not take any arguments and will not return anything
to the calling function.
#include<stdio.h>
#include<conio.h>
void sum( );
void main( )
clrscr( );
sum( );
getch( );
void sum( )
int a,b,c;
printf(“\n Enter 2 numbers”);
scanf(“%d%d”,&a,&b);
c= a+b;
printf(“\n Result = %d”,c);