238
UNIT - 5 FUNCTIONS AND POINTERS

Fucntions & Pointers in C

Embed Size (px)

Citation preview

Page 1: Fucntions & Pointers in C

UNIT - 5

FUNCTIONS AND POINTERS

Page 2: Fucntions & Pointers in C

FUNCTION

• Functions is a sub-program that contains one or more statements and it performs some task when called.

Page 3: Fucntions & Pointers in C

Types

Functions

User-DefinedFunctions

Pre-DefinedFunctions

Page 4: Fucntions & Pointers in C

Pre-Defined Functions • The pre-defined functions or library functions

are built-in functions.• The user can use the functions, but cannot

modify the function.• Example: sqrt()

Page 5: Fucntions & Pointers in C

User-Defined Functions • The functions defined by the user for their

requirement are called user-defined functions.• Whenever it is needed, The user can modify

the function.• Example: sum(a,b)

Page 6: Fucntions & Pointers in C

Advantage of User-Defined Functions

• The length of the source program can be reduced.

• It is easy to locate error.• It avoid coding of repeated instructions.

Page 7: Fucntions & Pointers in C

Elements of User-Defined Function

• Function declaration• Function definition• Function call

Page 8: Fucntions & Pointers in C

Function• Syntax

datatype function_name (parameters list) {local variable declaration;…………………………body of the function;…………………………return(expression);}

Page 9: Fucntions & Pointers in C

How Function Works

• Once a function is called the control passes to the called function.

• The working of calling function is temporarily stopped.

• When the execution of called function is completed then the control return back to the calling function and execute the next statement.

Page 10: Fucntions & Pointers in C
Page 11: Fucntions & Pointers in C

Parameters

• Actual ParameterThese are the parameters transferred

from the calling function to the called function.

• Formal ParameterThese are the parameters which is used

in the called function.

Page 12: Fucntions & Pointers in C
Page 13: Fucntions & Pointers in C

return Statement

• The return statement may or may not send some values to the calling function.

• Syntax:return; (or)return(expression);

Page 14: Fucntions & Pointers in C

Function Prototypes

• Function with no arguments and no return values.

• Function with arguments and no return values.

• Function with arguments and return values.• Function with no arguments and with return

values.

Page 15: Fucntions & Pointers in C

Function with no argumentsand no return values

• Here no data transfer take place between the calling function and the called function.

• These functions act independently, i.e. they get input and display output in the same block.

Page 16: Fucntions & Pointers in C
Page 17: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main() //calling function{

void add(void);add();

}void add() //called function{ int a,b,c; printf("\nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; printf("\nSum is:%d",c);}

Page 18: Fucntions & Pointers in C

Output

Enter two number:34

Sum is:7

Page 19: Fucntions & Pointers in C

Function with argumentsand no return values

• Here data transfer take place between the calling function and the called function.

• It is a one way data communication, i.e. the called program receives data from calling program but it does not return any value to the calling program.

Page 20: Fucntions & Pointers in C
Page 21: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int a,b;void add(int,int);printf("\nEnter two number:");scanf("%d%d",&a,&b);add(a,b);

}void add(int x,int y) //function with arguments{ int z; z=x+y; printf("\nSum is:%d",z);}

Page 22: Fucntions & Pointers in C

Output

Enter two number:24

Sum is:6

Page 23: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int a,b;void add(int a,int b);printf("\nEnter two number:");scanf("%d%d",&a,&b);add(a,b);

}void add(int x,int y) //function with arguments{ int z; z=x+y; printf("\nSum is:%d",z);}

Page 24: Fucntions & Pointers in C

Output

Enter two number:24

Sum is:6

Page 25: Fucntions & Pointers in C

Function with argumentsand return values

• Here data transfer take place between the calling function and the called function as well as between called function and calling function .

• It is a two way data communication, i.e. the called program receives data from calling program and it return some value to the calling program.

Page 26: Fucntions & Pointers in C
Page 27: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int a,b,c;int add(int,int);printf("\nEnter two number:");scanf("%d%d",&a,&b);c=add(a,b);printf("\nSum is:%d",c);

}int add(int x,int y){ int z; z=x+y; return(z);}

Page 28: Fucntions & Pointers in C

Output

Enter two number:67

Sum is:13

Page 29: Fucntions & Pointers in C

Function with no argumentsand with return values

• Here data transfer take place between the called function and the calling function.

• It is a one way data communication, i.e. the called program does not receives data from calling program but it return some value to the calling program.

Page 30: Fucntions & Pointers in C
Page 31: Fucntions & Pointers in C

#include <stdio.h>#include<conio.h>void main(){

int add(),d;d=add();printf("\nSum is:%d",d);

}int add() //function wit no argument{ int a,b,c; printf("\nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; return(c);}

Page 32: Fucntions & Pointers in C

Output

Enter two number:58

Sum is:13

Page 33: Fucntions & Pointers in C

Parameter Passing Methods

• Call by value• Call by reference

Page 34: Fucntions & Pointers in C

Call by value

• Actual argument passed to the formal argument.

• Any changes to the formal argument does not affect the actual argument.

Page 35: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int x,y,change(int,int);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);

Page 36: Fucntions & Pointers in C

change(x,y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}int change(int a,int b){ int c; c=a; a=b; b=c; printf("\nValues in the Fuction -->x=%d,y=%d",a,b);}

Page 37: Fucntions & Pointers in C

Output

Enter value of x:5

Enter value of y:6

Values in the Fuction -->x=6,y=5

Values in the Main()-->x=5,y=6

Page 38: Fucntions & Pointers in C

Call by reference

• Instead of passing value, the address of the argument will be passed.

• Any changes to the formal argument will affect the actual argument.

Page 39: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int x,y,change(int*,int*);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);

Page 40: Fucntions & Pointers in C

change(&x,&y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}int change(int *a,int *b){ int c; c=*a;

*a=*b; *b=c; printf("\nValues in the Function -->x=%d,y=%d",*a,*b);}

Page 41: Fucntions & Pointers in C

Output

Enter value of x:5

Enter value of y:6

Values in the Function -->x=6,y=5

Values in the Main()-->x=6,y=5

Page 42: Fucntions & Pointers in C

Recursion• It is a process of calling the same function

itself again and again until some condition is satisfied.

• Syntax:func1(){

………..func1();

}

Page 43: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int a; int rec(int); printf("\nEnter the number:"); scanf("%d",&a); printf("The factorial of %d! is %d",a,rec(a));}

Page 44: Fucntions & Pointers in C

int rec(int x){ int f; if(x==1)

return(1); else

f=x*rec(x-1); return(f);}

Output:Enter the number:5The factorial of 5! is 120

Page 45: Fucntions & Pointers in C

Example: Working of 3!

Page 46: Fucntions & Pointers in C

Tower of Honoi

123

323

1323

Page 47: Fucntions & Pointers in C

Tower of Honoi

123

323

1323

Page 48: Fucntions & Pointers in C

Library Function

• It is pre-defined function.• The library function provides functions like

mathematical, string manipulation etc,.

Page 49: Fucntions & Pointers in C

Examplesqrt(x):

It is used to find the square root of xExample: sqrt(36) is 6abs(x):

It is used to find the absolute value of xExample: abs(-36) is 36pow(x,y):

It is used to find the value of xy

Example: pow(5,2) is 25ceil(x):

It is used to find the smallest integer greater than or equal to x

Example: ceil(7.7) is 8

Page 50: Fucntions & Pointers in C

rand():It is used to generate a random number.

sin(x):It is used to find the sine value of x

Example: sin(30) is 0.5cos(x):

It is used to find the cosine value of xExample: cos(30) is 0.86tan(x):

It is used to find the tan value of xExample: tan(30) is 0.577

Page 51: Fucntions & Pointers in C

toascii(x):It is used to find the ASCII value of x

Example: toascii(a) is 97toupper(x):

It is used to convert lowercase character to uppercase.

Example: toupper(‘a’) is A toupper(97) is A

tolower(x):It is used to convert uppercase

character to lowercase.Example: tolower(‘A’) is a

Page 52: Fucntions & Pointers in C

Example:#include<stdio.h>#include<conio.h>#include<math.h>#include<ctype.h>void main(){ int x,y=2;

printf("\nEnter the number:"); scanf("%d",&x); printf("\nThe squareroot of %d is %f",x,sqrt(x));

printf("\nThe value of %d power%dis%f ",x,y,pow(6,2));

Page 53: Fucntions & Pointers in C

printf("\nThe ceiling of 6.7 is %f",ceil(6.7)); printf("\nThe floor of 6.7 is %f",floor(6.7)); printf("\nThe absolute value of -6 is %d",abs(-6)); printf("\nThe value of sin 45 is %f",sin(45)); printf("\nThe uppercase of 'a' is %c",toupper('a')); printf("\nThe uppercase of 97 is %c",toupper(97));

getch();}

Page 54: Fucntions & Pointers in C

Output:Enter the number:6

The squareroot of 6 is 2.449490The value of 6 power 2 is 36.000000The ceiling of 6.7 is 7.000000The floor of 6.7 is 6.000000The absolute value of -6 is 6The value of sin 45 is 0.850904The uppercase of 'a' is AThe uppercase of 97 is A

Page 55: Fucntions & Pointers in C

Array

• An Array is a collection of similar data items, that are stored under a common name.

• Types– One-Dimensional array– Two-Dimensional array– Multi-Dimensional array

Page 56: Fucntions & Pointers in C

One-Dimensional array Array Declaration

• Syntax:data_type array_name[size];

Example: int x[3];

X[0]

X[1]

X[2]

x

Page 57: Fucntions & Pointers in C

Array initialization

• At compile time• At run time

Page 58: Fucntions & Pointers in C

At compile time

• Syntax:data_type array_name[size]={variables};

Example: int x[3]={5,3,7};

5

3

7

X[0]

X[1]

X[2]

x

Page 59: Fucntions & Pointers in C

At Run time• Array can also initialize at the run time.• Example:

while(i<10){

if(i<5)sum[i]=0;

elsesum[i]=sum[i]+i;

}

Page 60: Fucntions & Pointers in C

• Example:

scanf(“%d%d”,&a[0],&a[1]);

Page 61: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int x[2],i; printf("\nEnter the inputs:"); for(i=0;i<2;i++)

scanf("%d",&x[i]); for(i=0;i<2;i++)

printf("\nThe value in x[%d] is %d",i,x[i]); getch();}

Page 62: Fucntions & Pointers in C

Output

Enter the inputs:36

The value in x[0] is 3The value in x[1] is 6

Page 63: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int i; char x[5]={'a','b','c','d','e'}; clrscr(); for(i=0;i<5;i++)

printf("\nThe value in x[%d] is %c",i,x[i]); getch();}

Page 64: Fucntions & Pointers in C

Output

The value in x[0] is aThe value in x[1] is bThe value in x[2] is cThe value in x[3] is dThe value in x[4] is e

Page 65: Fucntions & Pointers in C

Two-Dimensional array Array Declaration

• Syntax:data_type array_name[row_size]

[col_size];Example: int x[3][2];

X[0][0]

X[1][0]

X[2][0]

Col 0 Col 1

row 0

row 1

row 2

X[0][1]

X[1][1]

X[2][1]

Page 66: Fucntions & Pointers in C

Array Initialization

• Syntax:data_type array_name[row_size] [col_size];={variables};

Example: int x[2][2]={1,50,2,75};

Page 67: Fucntions & Pointers in C

int x[2][2]={ {1,50},{2,75}

};

(or)

int x[ ][2]={ {1,50},{2,75}

};

Page 68: Fucntions & Pointers in C

1 50

2 75

row 0

row 1

Col 0 Col 1

Page 69: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int i,j; int x[2][2]={ {1,50},

{2,75}};

clrscr(); for(i=0;i<2;i++)

for(j=0;j<2;j++)printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);

getch();}

Page 70: Fucntions & Pointers in C

Output

The value in x[0][0] is 1The value in x[0][1] is 50The value in x[1][0] is 2The value in x[1][1] is 75

Page 71: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int i,j; int x[][2]={ {1,50},{2,75},{3,65}}; clrscr(); for(i=0;i<=2;i++)

for(j=0;j<2;j++)printf("\nThe value in x[%d][%d] is %d",i,j,x[i][j]);

getch();}

Page 72: Fucntions & Pointers in C

Output

The value in x[0][0] is 1The value in x[0][1] is 50The value in x[1][0] is 2The value in x[1][1] is 75The value in x[2][0] is 3The value in x[2][1] is 65

Page 73: Fucntions & Pointers in C

Matrix Addition #include<stdio.h>#include<conio.h>void main(){ int i,j,k,r1,r2,c1,c2; int a[5][5],b[5][5],c[5][5]; clrscr(); step1: printf("\n Enter the size of matrix A:"); scanf("%d%d",&r1,&c1); printf("\n Enter the size of matrix B: "); scanf("%d%d",&r2,&c2); if((c1==c2)&&(r1==r2))

goto step2; else

goto step1;

Page 74: Fucntions & Pointers in C

step2: printf("\n Enter the elements of matrix A \n"); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++){scanf("%d",&a[i][j]);}

} printf("\n Enter the elements of matrix B \n"); for(i=0;i<r2;i++) {

for(j=0;j<c2;j++){scanf("\t%d",&b[i][j]);}

}

Page 75: Fucntions & Pointers in C

for(i=0;i<r1;i++) {

for(j=0;j<c1;j++){c[i][j]=0; c[i][j]=c[i][j]+a[i][j]+b[i][j];}

} printf("\n The resultant matrix after addition of A & B is\n"); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++)printf("%d\t",c[i][j]);printf("\n");

} getch();}

Page 76: Fucntions & Pointers in C

OutputEnter the size of matrix A: 22 Enter the size of matrix B: 22 Enter the elements of matrix A2222 Enter the elements of matrix B3333 The resultant matrix after addition of A&B is5 55 5

Page 77: Fucntions & Pointers in C

Matrix Multiplication#include<stdio.h>#include<conio.h>void main(){ int i,j,k,r1,r2,c1,c2; int a[5][5],b[5][5],c[5][5]; clrscr(); step1: printf("\n Enter the size of matrix A \n"); scanf("%d%d",&r1,&c1); printf("\n Enter the size of matrix B \n"); scanf("%d%d",&r2,&c2); if(c1==r2)

goto step2; else

goto step1;

Page 78: Fucntions & Pointers in C

step2: printf("\n Enter the elements of matrix A \n"); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++){scanf("%d",&a[i][j]);}

} printf("\n Enter the elements of matrix B \n"); for(i=0;i<r2;i++) {

for(j=0;j<c2;j++){scanf("\t%d",&b[i][j]);}

}

Page 79: Fucntions & Pointers in C

for(i=0;i<r1;i++) {

for(j=0;j<c2;j++){c[i][j]=0;for(k=0;k<c1;k++){c[i][j]=c[i][j]+a[i][k]*b[k][j];}}

} for(i=0;i<r1;i++) {

for(j=0;j<c2;j++)printf("%d\t",c[i][j]);printf("\n");

} getch();}

Page 80: Fucntions & Pointers in C

Output Enter the size of matrix A:22 Enter the size of matrix B:22 Enter the elements of matrix A4444 Enter the elements of matrix B4444 The resultant matrix is32 3232 32

Page 81: Fucntions & Pointers in C

Enter the size of matrix A:23 Enter the size of matrix B:32 Enter the elements of matrix A123456 Enter the elements of matrix B246824

Page 82: Fucntions & Pointers in C

20 3250 80

Page 83: Fucntions & Pointers in C

Passing array to Function• Here an array is transferred as parameter to a

function.• void main() void fun(n,b[])

{ {void fun(int,int); int x,b[5];int a[5],n; …………..…………… …………..fun(n,a);…………… }}

Page 84: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void add(int,int b[]);void main(){ int a[5],i,n; clrscr(); printf("\n Enter the Number: "); scanf("%d",&n); printf("\n Enter the Values: ");

for(i=0;i<n;i++)scanf("%d",&a[i]); add(n,a);

}

Page 85: Fucntions & Pointers in C

void add(int x,int b[]) { int sum=0,i; for(i=0;i<x;i++)

sum=sum+b[i]; printf("\nThe sum is: %d",sum); }

Page 86: Fucntions & Pointers in C

Output Enter the Number: 5

Enter the Values: 12345

The sum is: 15

Page 87: Fucntions & Pointers in C

Array of Characters

• In array the characters are terminated by the null (‘\0’) character.

• Example: char a[]={a,b,c};

a b c \0

Page 88: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int i=0; char a[]="abcd"; clrscr(); while(a[i]!='\0') {

printf("\t%c",a[i]);i++;

}}

Page 89: Fucntions & Pointers in C

Output

a b c d

Page 90: Fucntions & Pointers in C

Multi Dimensional Array

• Syntaxdatatype array_name [size1][size2]….[size n]

datatype - type of the data.array_name -name of the array.size -size of the array.

Page 91: Fucntions & Pointers in C

Example:int a[3][3][3];

Col 0 Col 1 Col 2

row 0

row 1

row 2

X[0][0]

X[1][0]

X[2][0]

X[0][1]

X[1][1]

X[2][1]

X[0][2]

X[1][2]

X[2][2]

Page 92: Fucntions & Pointers in C

String Functions• strlen()

It is used to find the length of the string.syntax:

strlen(string)• strcpy()

It is used to copy one string to another.syntax:

strcpy(string1,string2)• strcat()

It is used to combine two strings.syntax:

strcat(string1,string2)

Page 93: Fucntions & Pointers in C

• strcmp()It is used to compare two strings.

syntax:strcmp(string1,string2)– Returns 0 if two strings are equal.– Return value <0 if s1 is less than s2.– Return value >0 if s1 is greater than s2.

• strrev() It used to reverse a string.

syntax:strrev(string)

• strlwr(), strupr()It used to change the case of a string.

syntax:strlwr(string)strupr(string)

Page 94: Fucntions & Pointers in C

• strncpy()It used to copy ‘n’ characters of one string to

another. • strstr()

– It is used to determine the first occurrence of a given string in another string.

• strncat()– It Appends source string to destination string

upto specified length.• strspn()

– It is used t find upto what length two strings are identical.

Page 95: Fucntions & Pointers in C

• strncmp()– It is used to compare ‘n’ character of two strings.

• strcmpi()– It is used to compare two strings without regarding the case.

• strnicmp()– It is used to compare first ‘n’ characters of two strings

without regarding the case.• strchr()

– It is used to determine the first occurrence of a given character in a string.

• strrchr()– It is used to determine the last occurrence of a given

character in a string.

Page 96: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char a[]="college"; int b; clrscr(); b=strlen(a); printf("\nThe length of the string is %d",b); getch();}

Output:The length of the string is 7

Page 97: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char a[]="IT"; char b[]="Dept"; clrscr(); strcpy(a,b); printf("\nThe string is %s",a); getch();}

Output:The string is Dept

Page 98: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char a[]="IT"; char b[]="Dept"; clrscr(); strcat(a,b); printf("\nThe string is %s",a); getch();}

Output:The string is ITDept

Page 99: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){char a[]="itdept";char b[]="it";int i;clrscr();i=strcmp(a,b);if(i==0)

printf("\nstrings are equal:%d",i);else if(i<0)

printf("\nstring1 is less than string2:%d",i);

Page 100: Fucntions & Pointers in C

elseprintf("\nstring1 is greater than string2:%d",i);

getch();}

Output:string1 is greater than string2:100

Page 101: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){char a[]="itdept";clrscr();printf("\nThe string is :%s",a);strupr(a);printf("\nThe string after conversion to uppercase :%s",a);strlwr(a);printf("\nThe string after conversion to lowercase :%s",a); getch();}

Page 102: Fucntions & Pointers in C

Output

The string is :itdeptThe string after conversion to uppercase :ITDEPTThe string after conversion to lowercase :itdept

Page 103: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char a[]="Dept";clrscr();printf("\nThe string is %s",strrev(a)); getch();}

Output:The string is tpeD

Page 104: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){char a[]="itdept";char b[15];int i=0;clrscr();strncpy(b,a,2); b[2]='\0';printf("\nThe string is :%s",b); getch();}

Page 105: Fucntions & Pointers in C

Output:The string is :it

Page 106: Fucntions & Pointers in C

String Palindrome#include<stdio.h>#include<conio.h>#include<string.h>void main(){

int len,i,j;char str[15];clrscr();printf("\n Enter the string:");scanf("%s",str);len=strlen(str);

Page 107: Fucntions & Pointers in C

for(i=0,j=len-1;i<len/2;i++,j--){ if(str[i]!=str[j]) { printf("\nThe String is not a palindrome"); getch(); exit(0); }}printf("\nThe String is a palindrome");getch();

}

Output: Enter the string:abcbaThe String is a palindrome

Page 108: Fucntions & Pointers in C

Enumerated Data Type• It is user defined data type.• The user can create their own data type and

define some values to the variables.• Syntax:

enum tag_name{

enum1;enum2;……….

};

Page 109: Fucntions & Pointers in C

Example#include <conio.h>#include <stdio.h>void main(){

enum week {sun,mon,tue,wed,thr,fri,sat};clrscr();printf("\nMonday=%d",mon);printf("\nSaturday=%d",sat);

getch();}

Output:Monday=1Saturday=6

Page 110: Fucntions & Pointers in C

Example#include <conio.h>#include <stdio.h>void main(){

enum week {sun=10,mon,tue,wed,thr,fri,sat};clrscr();printf("\nMonday=%d",mon);printf("\nsaturday=%d",sat);

getch();}

Output:Monday=11saturday=16

Page 111: Fucntions & Pointers in C

Structure• A Structure is a collection of different data

items, that are stored under a common name.• Syntax:

struct structure_name{

structure element1;structure element2;…………………….

};

Page 112: Fucntions & Pointers in C

• Example:struct stud{

int sno;char name[10];int mark;

};

struct stud s;

Page 113: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>struct stud

{int regno;char name[10];int m1;int m2;int m3;};

struct stud s;void main(){ float tot,avg;

Page 114: Fucntions & Pointers in C

printf("\nEnter the student regno,name,m1,m2,m3:"); scanf("%d%s%d%d%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3);

tot=s.m1+s.m2+s.m3; avg=tot/3; printf("\nThe student Details are:"); printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg);

}

Page 115: Fucntions & Pointers in C

Output

Enter the student regno,name,m1,m2,m3:100aaa879878

The student Details are:100 aaa 263.000000 87.666664

Page 116: Fucntions & Pointers in C

Structure assignment

• It is possible to assign one structure information to another structure of same type using simple assignment statement.

Page 117: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){

struct { int a; }x,y; clrscr(); x.a=10; y=x; printf("The value of y.a is%d",y.a); getch();

}

Page 118: Fucntions & Pointers in C

Output

The value of y.a is10

Page 119: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>struct stud

{int regno;char name[10],grade;int m1,m2,m3;float avg,tot;} s[10];

void main(){ int i,n;printf("\nEnter the no.of students:"); scanf("%d",&n);

Page 120: Fucntions & Pointers in C

for(i=0;i<n;i++) {printf("\nEnter the student regno,name,m1,m2,m3:");scanf("%d%s%d%d%d",&s[i].regno,&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3); s[i].tot=s[i].m1+s[i].m2+s[i].m3; s[i].avg=s[i].tot/3; if(s[i].m1<35||s[i].m2<35||s[i].m3<35)

s[i].grade='f'; else {

if(s[i].avg>=75) s[i].grade='d';else if(s[i].avg>=60)

Page 121: Fucntions & Pointers in C

s[i].grade='A';else if(s[i].avg>=50)

s[i].grade='B';else if(s[i].avg>=35)

s[i].grade='C'; } } printf("\nSTUDENT MARK LIST\n");printf("\nREGNO\tNAME\tTOTAL\tAvg\tGRADE");for(i=0;i<n;i++) printf("\n%d\t%s\t%f\t%f\t

%c",s[i].regno,s[i].name,s[i].tot,s[i].avg,s[i].grade); getch(); }

Page 122: Fucntions & Pointers in C

Enter the no.of students:2Enter the student regno,name,m1,m2,m3:101aaa899878Enter the student regno,name,m1,m2,m3:102bbb596876STUDENT MARK LIST

REGNO NAME TOTAL Avg GRADE101 aaa 265.000000 88.333336 d102 bbb 203.000000 67.666664 A

Page 123: Fucntions & Pointers in C

Union• An Union is a collection of different data items,

that are stored under a common name. Here same memory is shared by its members.

• Syntax:union union _name{

union element1; union element2;…………………

};

Page 124: Fucntions & Pointers in C

• Example: union result{

int mark; float avg;char grade;

};

union result s;

Page 125: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>union stud

{int a;char b[2];};

void main(){

union stud c;

Page 126: Fucntions & Pointers in C

c.a=256;printf("\nc.a value is%d",c.a);printf("\nc.b[0] value is%d",c.b[0]);printf("\nc.b[1] value is%d",c.b[1]);

}

Output:c.a value is256c.b[0] value is0c.b[1] value is1

Page 127: Fucntions & Pointers in C

• 256 = 00000010 00000000

Higher bit Lower bit

00000000 00000010

c.a - 2 Byte

c.b[0] 1 Byte c.b[0] 1 Byte

c.b[0] c.b[1]

Page 128: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>struct student{ int a; int b; char c;}s;union student1{ int a; int b; char c;}s1;

Page 129: Fucntions & Pointers in C

void main(){printf("\nThe size of struct is %d",sizeof(s));printf("\nThe size of union is %d",sizeof(s1));getch();}

Output:The size of struct is 5The size of union is 2

Page 130: Fucntions & Pointers in C

Structure & Union

int int char

2 Byte 2Byte 1Byte

2 Byte

structure

int ,int, char

union

Page 131: Fucntions & Pointers in C

Preprocessor

• It is a program that processes the source program before compilation.

• It operates under the following directives–File Inclusion–Macro substitution–Conditional inclusion

Page 132: Fucntions & Pointers in C

File Inclusion

• It is used to include some file that contains functions or some definitions.

• Syntax:#include<filename> (or)#include“filename”

• Eg: #include<stdio.h> #include “ex.c”

Page 133: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include "addition.txt"void main(){ int a,b; printf("\nEnter the numbers:"); scanf("%d%d",&a,&b); printf("The Value is %d",add(a,b)); getch();}

Page 134: Fucntions & Pointers in C

addition.txt

int add(int a,int b){ return(a+b);}

Page 135: Fucntions & Pointers in C

Output

Enter the numbers:74The Value is 11

Page 136: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include "fact.c"void main(){ int a; printf("\nEnter the number:"); scanf("%d",&a); printf("The factorial of %d! is %d",a,rec(a)); getch();}

Page 137: Fucntions & Pointers in C

fact.cint rec(int x){ int f; if(x==1)

return(1); else

f=x*rec(x-1); return(f);}

Page 138: Fucntions & Pointers in C

Output

Enter the number:5The factorial of 5! is 120

Page 139: Fucntions & Pointers in C

Macro Substitution

• It is used to define and use integer, string, or identifier in the source program

• The three forms of macros are–Simple Macro–Argumented Macro–Nested Macro

Page 140: Fucntions & Pointers in C

Simple Macro

• It is used to define some constants• Syntax

# define identifier string/integer• Eg:

#define pi 3.14#define CITY “chennai”

Page 141: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#define pi 3.14#define CITY "chennai"void main(){ printf("The Value is %f",2*pi); printf("\nThe Value CITY is %s",CITY); getch();}

Output:The Value is 6.280000The Value CITY is chennai

Page 142: Fucntions & Pointers in C

Argumented Macro

• It is used to define some complex forms in the source program.

• Syntax:#define identifier (v1,v2,….) string/integer

• Eg:#define cube(n) (n*n*n)

Page 143: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#define cube(n) (n*n*n)void main(){ printf("The Value of 3 cube is %d",cube(3)); getch();}

Output:The Value of 3 cube is 27

Page 144: Fucntions & Pointers in C

Nested Macro

• Here one macro is used by another macro.

• Eg:#define a 3#define sq a*a

Page 145: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#define a 3#define sq a*avoid main(){ printf("The Value is %d",sq); getch();}

Output:The Value is 9

Page 146: Fucntions & Pointers in C

Conditional Inclusion

• It is used to include some conditional statements.

Page 147: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#define a 3#ifdef a#define c a+5#endifvoid main(){ printf("\nThe value C is %d",c);

getch();}

Output:The value C is 8

Page 148: Fucntions & Pointers in C

Pointers

• Pointer is a variable that contains the memory address of another variable.

Page 149: Fucntions & Pointers in C

Example: x=5

x Variable

1002 Address

5 Value

Page 150: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int x=5; printf("\n The Address of x = %u",&x); printf("\n The Value of x = %d",x);}

OutputThe Address of x = 8714The Value of x = 5

Page 151: Fucntions & Pointers in C

Pointer Declaration• Syntax

data-type *pointer-name;

data-type - Type of the data to which the pointer points.

pointer-name - Name of the pointer

• Example: int *a;

Page 152: Fucntions & Pointers in C

Accessing Variable through Pointer

• If a pointer is declared and assigned to a variable, then the variable can be accessed through the pointer.

• Example:int *a;x=5;a=&x;

Page 153: Fucntions & Pointers in C

• Example

#include<stdio.h>#include<conio.h>void main(){ int x=5; int *a; a=&x; printf("\n The Value of x = %d",x); printf("\n The Address of x = %u",&x); printf("\n The Value of a = %d",a); printf("\n The Value of x = %d",*a);}

Page 154: Fucntions & Pointers in C

Output

The Value of x = 5 The Address of x = 8758 The Value of a = 8758 The Value of x = 5

Page 155: Fucntions & Pointers in C

Example:#include<stdio.h>#include<conio.h>void main(){ int y=10; int *a; a=&y; printf("\n The Value of y = %d",y); printf("\n The Address of y = %u",&y); printf("\n The Value of a = %d",a); printf("\n The Address of a = %u",&a);}

Page 156: Fucntions & Pointers in C

5001 10

8000

a y

5001

Variable

Value

Address

Page 157: Fucntions & Pointers in C

Output

The Value of y = 10The Address of y = 5001The Value of a = 5001The Address of a = 8000

Page 158: Fucntions & Pointers in C

Null Pointer

• A pointer is said to be null pointer if zero is assigned to the pointer.

• Exampleint *a,*b;a=b=0;

Page 159: Fucntions & Pointers in C

Pointer to Pointer

• Here one pointer stores the address of another pointer variable.

• Example:int x=10,*a,**b;a=&x;b=&a;

Page 160: Fucntions & Pointers in C

5001 10

8000

a x

5001

Variable

Value

Address

8000

9000

b

Page 161: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int a=10; int *b,**c; b=&a; c=&b; printf("\n The Value of a = %d",a); printf("\n The Address of a = %u",&a); printf("\n The Value of b = %d",b); printf("\n The Address of b = %u",&b); printf("\n The Value of c = %d",c); printf("\n The Address of c = %u",&c);}

Page 162: Fucntions & Pointers in C

Output

The Value of a = 10 The Address of a = 5001 The Value of b = 5001 The Address of b = 8000 The Value of c = 8000 The Address of c = 9000

Page 163: Fucntions & Pointers in C

Pointers and Functions

• Call by Value• Call by Reference

Page 164: Fucntions & Pointers in C

Call by value

• Actual argument passed to the formal argument.

• Any changes to the formal argument does not affect the actual argument.

Page 165: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int x,y,swap(int,int);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);

Page 166: Fucntions & Pointers in C

change(x,y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}int swap(int a,int b){ int c; c=a; a=b; b=c; printf("\nValues in the Function -->x=%d,y=%d",a,b);}

Page 167: Fucntions & Pointers in C

Output

Enter value of x:5

Enter value of y:6

Values in the Function -->x=6,y=5

Values in the Main()-->x=5,y=6

Page 168: Fucntions & Pointers in C
Page 169: Fucntions & Pointers in C

Call by reference

• Instead of passing value, the address of the argument will be passed.

• Any changes to the formal argument will affect the actual argument.

Page 170: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int x,y,change(int*,int*);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);

Page 171: Fucntions & Pointers in C

change(&x,&y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}int change(int *a,int *b){ int c; c=*a;

*a=*b; *b=c; printf("\nValues in the Function -->x=%d,y=%d",*a,*b);}

Page 172: Fucntions & Pointers in C

Output

Enter value of x:5

Enter value of y:6

Values in the Function -->x=6,y=5

Values in the Main()-->x=6,y=5

Page 173: Fucntions & Pointers in C

Pointer to Array

• The elements of the array can also be accessed through a pointer.

• Exampleint a[3]={2,3,7};int *b;b=a;

Page 174: Fucntions & Pointers in C

Example:#include<stdio.h>#include<conio.h>void main(){ int a[3]={2,3,7}; int *b; b=a; printf("\n The Value of a[0] = %d",a[0]); printf("\n The Address of a[0] = %u",&a[0]); printf("\n The Value of b = %d",b);}

Page 175: Fucntions & Pointers in C

8744 2

9000

b a[0]

8744

Variable

Value

Address

Page 176: Fucntions & Pointers in C

Output

The Value of a[0] = 2 The Address of a[0] = 8744 The Value of b = 8744

Page 177: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int a[5]={2,3,7,9,10}; int i; for(i=0;i<5;i++) { printf("\n The Value of a[%d] = %d",i,a[i]); printf("\n The Address of a[%d] = %u",i,&a[i]); } }

Page 178: Fucntions & Pointers in C

2 3 7 9 10

a[0] a[1] a[2] a[3] a[4]

8724 8726 8728 8730 8732

Array

Value

Address

Page 179: Fucntions & Pointers in C

Output The Value of a[0] = 2 The Address of a[0] = 8724 The Value of a[1] = 3 The Address of a[1] = 8726 The Value of a[2] = 7 The Address of a[2] = 8728 The Value of a[3] = 9 The Address of a[3] = 8730 The Value of a[4] = 10 The Address of a[4] = 8732

Page 180: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(){ int a[5]={1,2,3,4,5}; int i,sum=0; int *b; b=a; for(i=0;i<5;i++) { sum=sum + *b; b++; //b=b+1 } printf("\n The Sum is %d",sum);}

Page 181: Fucntions & Pointers in C

Output

The Sum is 15

Page 182: Fucntions & Pointers in C

Pointer and Structures

• Syntax:struct structure_name{

structure element1;structure element2;…………………….

}variable,*ptr;

Page 183: Fucntions & Pointers in C

• Example:struct stud{

int sno;char name[10];int mark;

};

struct stud *s;

Page 184: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>struct stud

{int regno;char name[10];int m1;int m2;int m3;};

struct stud s;struct stud *t;

Page 185: Fucntions & Pointers in C

void main(){ float tot,avg; t=&s;printf("\nEnter the student regno,name,m1,m2,m3:");scanf("%d%s%d%d

%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3); tot=s.m1+s.m2+s.m3; avg=tot/3; printf("\nThe student Details are:"); printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg); printf("\n%d\t%s\t%f\t%f",t->regno,t->name,tot,avg); }

Page 186: Fucntions & Pointers in C

Output

Enter the student regno,name,m1,m2,m3:1aaa768976

The student Details are:1 aaa 241.000000 80.3333361 aaa 241.000000 80.333336

Page 187: Fucntions & Pointers in C

Command Line Argument

• It allows the user to pass some information to the program while running the program.

Page 188: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>void main(int argc,char argv[]){ printf("\n The Argument is %s",argv[0]); getch();}

Page 189: Fucntions & Pointers in C

Output

C:\tc>a The Argument is C:\TC\A.EXE

Page 190: Fucntions & Pointers in C

String Palindrome#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char s1[15],s2[15]; printf("\nenter the string:"); scanf("%s",s1); strcpy(s2,s1); strrev(s1);

Page 191: Fucntions & Pointers in C

if(strcmp(s1,s2)==0)printf("\n The string is palindrome");

else printf("\n The string is not a palindrome");

getch();}

Output:enter the string: aba

The string is palindrome

Page 192: Fucntions & Pointers in C

Developing a ‘C’ Program

• The Program development life cycle is considered as a sequence of events by the programmer to develop the program.

• The Program development life cycle contains the following phase– Program Design– Program Coding– Program Testing

Page 193: Fucntions & Pointers in C

Program Design

• Analysing the problem• Algorithm development• Selection of conditional and control structure

etc,.

Page 194: Fucntions & Pointers in C

Program Coding

• Documentation• Statement construction• Input and output format etc,.

Page 195: Fucntions & Pointers in C

Program Testing

• It is the process of executing the program with sample data

Page 196: Fucntions & Pointers in C
Page 197: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#define p 3.14void main(){#ifdef pprintf("\nPentium");#elseprintf("\n Celeron");#endif printf("\nthe value is %f",2*p); getch();}

Output:Pentiumthe value is 6.280000

Page 198: Fucntions & Pointers in C
Page 199: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>struct stud

{int regno;char name[10];int m1;int m2;int m3;};

struct stud s;void main(){ float tot,avg; int i;

Page 200: Fucntions & Pointers in C

printf("\nEnter the student regno,name,m1,m2,m3:"); for(i=0;i<2;i++) { scanf("%d%s%d%d

%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3); tot=s.m1+s.m2+s.m3; avg=tot/3; } for(i=0;i<2;i++) printf("%d\t%s\t%f\t%f",s.regno,s.name,tot,avg); }

Page 201: Fucntions & Pointers in C

Enter the student regno,name,m1,m2,m3:100aaa786798101bbb809075101bbb 245.000000 81.666664101bbb 245.000000 81.666664

Page 202: Fucntions & Pointers in C

#include <string.h>#include <stdio.h>int main(void){ char string[15]; char *ptr, c = 'r'; strcpy(string, "This is a string"); ptr = strchr(string, c); if (ptr) printf("The character %c is at position: %d\n", c, ptr-string); else printf("The character was not found\n"); return 0;}

Page 203: Fucntions & Pointers in C

Example#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char a[]="Dept"; int i=0;clrscr();while(a[i]!='\0'){ printf("\nThe character is %c",a[i]); i++;} getch();}

Page 204: Fucntions & Pointers in C

The character is DThe character is eThe character is pThe character is t

Page 205: Fucntions & Pointers in C
Page 206: Fucntions & Pointers in C

Structure

Page 207: Fucntions & Pointers in C
Page 208: Fucntions & Pointers in C
Page 209: Fucntions & Pointers in C
Page 210: Fucntions & Pointers in C

Lab Exercise

C Programs

Page 211: Fucntions & Pointers in C

Function-with arg & return#include <stdio.h>#include<conio.h>void main(){

int a,b,c;int add(int,int);printf("\nEnter two number:");scanf("%d%d",&a,&b);c=add(a,b);printf("\nSum is:%d",c);

}int add(int x,int y){ int z; z=x+y; return(z);}

Page 212: Fucntions & Pointers in C

Output

Enter two number:67

Sum is:13

Page 213: Fucntions & Pointers in C

Example#include <stdio.h>#include<conio.h>void main(){

int x,y,change(int*,int*);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);

Page 214: Fucntions & Pointers in C

change(&x,&y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);

}int change(int *a,int *b){ int c; c=*a;

*a=*b; *b=c; printf("\nValues in the Function -->x=%d,y=%d",*a,*b);}

Page 215: Fucntions & Pointers in C

Output

Enter value of x:5

Enter value of y:6

Values in the Function -->x=6,y=5

Values in the Main()-->x=6,y=5

Page 216: Fucntions & Pointers in C

Factorial-Recursive Fn#include<stdio.h>#include<conio.h>void main(){ int a; int rec(int); printf("\nEnter the number:"); scanf("%d",&a); printf("The factorial of %d! is %d",a,rec(a));}

Page 217: Fucntions & Pointers in C

int rec(int x){ int f; if(x==1)

return(1); else

f=x*rec(x-1); return(f);}

Output:Enter the number:5The factorial of 5! is 120

Page 218: Fucntions & Pointers in C

Example: Working of 3!

Page 219: Fucntions & Pointers in C

Matrix Multiplication#include<stdio.h>#include<conio.h>void main(){ int i,j,k,r1,r2,c1,c2; int a[5][5],b[5][5],c[5][5]; clrscr(); step1: printf("\n Enter the size of matrix A \n"); scanf("%d%d",&r1,&c1); printf("\n Enter the size of matrix B \n"); scanf("%d%d",&r2,&c2); if(c1==r2)

goto step2; else

goto step1;

Page 220: Fucntions & Pointers in C

step2: printf("\n Enter the elements of matrix A \n"); for(i=0;i<r1;i++) {

for(j=0;j<c1;j++){scanf("%d",&a[i][j]);}

} printf("\n Enter the elements of matrix B \n"); for(i=0;i<r2;i++) {

for(j=0;j<c2;j++){scanf("\t%d",&b[i][j]);}

}

Page 221: Fucntions & Pointers in C

for(i=0;i<r1;i++) {

for(j=0;j<c2;j++){c[i][j]=0;for(k=0;k<c1;k++){c[i][j]=c[i][j]+a[i][k]*b[k][j];}}

} for(i=0;i<r1;i++) {

for(j=0;j<c2;j++)printf("%d\t",c[i][j]);printf("\n");

} getch();}

Page 222: Fucntions & Pointers in C

Output Enter the size of matrix A:22 Enter the size of matrix B:22 Enter the elements of matrix A4444 Enter the elements of matrix B4444 The resultant matrix is32 3232 32

Page 223: Fucntions & Pointers in C
Page 224: Fucntions & Pointers in C

Lab Ex:9,Finding area, circumference of circle

#include<stdio.h>#include <conio.h>void main ( ){int r;float area,c;clrscr( );printf(" \nEnter the value of r:");scanf("%d",&r);

Page 225: Fucntions & Pointers in C

area=3.14*r*r;c=2*3.14*r;printf(" \nThe area is :%f",area);printf(" \nThe circumference is :%f",c);getch( );}

Output:Enter the value of r:7

The area is :153.860001The circumference is :43.959999

Page 226: Fucntions & Pointers in C

Lab Ex:9,Conversion of Celsius to Fahrenheit

#include<stdio.h>#include <conio.h>void main ( ){float c,f;clrscr( );printf(" \nEnter the value of c:");scanf("%f",&c);f=(c*1.8)+32;printf(" \nThe Fahrenheit is :%f",f);getch( );}

Page 227: Fucntions & Pointers in C

Enter the value of c:35

The fahrenheit is :95.000000

Page 228: Fucntions & Pointers in C

Lab Ex:11,Arithmetic operations#include<stdio.h>#include<conio.h>void main(){ int a,b,c,d,e,f; clrscr(); printf("\nEnter the values of A and B:"); scanf("%d%d",&a,&b); c=a+b; d=a-b; e=a*b; f=a/b;

Page 229: Fucntions & Pointers in C

printf("\nThe values of A + B:%d",c); printf("\nThe values of A - B:%d",d); printf("\nThe values of A * B:%d",e); printf("\nThe values of A / B:%d",f); getch();}

Page 230: Fucntions & Pointers in C

Output

Enter the values of A and B:63

The values of A + B:9The values of A - B:3The values of A * B:18The values of A / B:2

Page 231: Fucntions & Pointers in C

Lab.Ex13,Largest among 3 nos#include<stdio.h>#include <conio.h>void main ( ){int a,b,c;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);printf(" \nEnter the value of c:");scanf("%d",&c);

Page 232: Fucntions & Pointers in C

if((a>b)&&(a>c)){ printf(" \nA is Big");}else{

if(b>c)printf(" \nB is Big"); elseprintf(" \nC is Big");

}getch( );}

Page 233: Fucntions & Pointers in C

Output

Enter the value of a:5

Enter the value of b:7

Enter the value of c:3

B is Big

Page 234: Fucntions & Pointers in C

String Palindrome#include<stdio.h>#include<conio.h>#include<string.h>void main(){

int len,i,j;char str[15];clrscr();printf("\n Enter the string:");scanf("%s",str);len=strlen(str);

Page 235: Fucntions & Pointers in C

for(i=0,j=len-1;i<len/2;i++,j--){ if(str[i]!=str[j]) { printf("\nThe String is not a palindrome"); getch(); exit(0); }}printf("\nThe String is a palindrome");getch();

}

Output: Enter the string:abcbaThe String is a palindrome

Page 236: Fucntions & Pointers in C

Lab.Ex:14,Quadratic Equation#include<stdio.h>#include <conio.h>#include<math.h>void main ( ){int a,b,c,d,r1,r2;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);printf(" \nEnter the value of c:");scanf("%d",&c);d=b*b-4*a*c;

Page 237: Fucntions & Pointers in C

if(d>=0){

r1=(-b+sqrt(d))/(2*a);r2=(-b-sqrt(d))/(2*a);

printf(" \nThe roots are %d,%d",r1,r2);}else{ printf(" \nThe roots are imaginary");}getch( );}

Page 238: Fucntions & Pointers in C

Output

Enter the value of a:1

Enter the value of b:4

Enter the value of c:4

The roots are -2,-2