Sie sind auf Seite 1von 32

1.

Write a C program to check whether number is even or odd. Ans:

#include<stdio.h> main() { int num; printf ("Enter a number to be checked for even/odd: "); scanf ("%d",&num); if (num%2==0) { printf ("The entered number is EVEN.\n"); } else { printf ("The entered number is ODD.\n"); } }

Write a C program to illustrate the use of STATIC keyword and the scope of variables. Ans: #include <stdio.h>
2.

void func() { static int x = 0; // x is initialized only once across three calls of func() printf("%d\n", x); // outputs the value of x x = x + 1; } int main(int argc, char * const argv[]) { func(); // prints 0 func(); // prints 1 func(); // prints 2 return 0; }
3. Write a C program to check whether the given no is prime number or not? Ans: #include<stdio.h> void main() { int n,pflag=0; printf("enter the number \n"); scanf("%d",&n); if(2 <= (n/2)) { if(n%2) { if(n%3)

{ if(n%5) { if(n%7) { pflag = 1; } } } } } if(pflag) printf("%d is prime number \n",n); else printf("%d is not a prime number \n",n); } //============================================================================

//if u need to know below 100 prime numbers.

#include<stdio.h> #include<conio.h> void main() { int n,i=2; clrscr(); printf("Enter a number \n"); scanf("%d",&n); while(i<=n/2) { if(n % i == 0) { printf("Not a Prime Number"); getch(); exit(0); } i=i+1; } printf("Prime Number"); getch(); exit(0); } ---------------------------------------------------NB: n % i == 0 -------> here the we get the remainder when n is divided by each i, at any time the remainder is 0, the program will terminate and show a message " not prime". otherwise it will execute till i reaches to n/2. and terminate the program and shows a message "prime".
4. //=============================================================== Write a C program to print all the prime numbers for 1 to 100.? Ans:
#include <stdio.h> void main() { int a[100],i,j=2;

for(i=0;i<100;i++) a[i]=i+1; printf("Prime Number between 1 to 100 \n"); for(i=0;i<100;i++) { for(j=2;a[i]>j ; j++) { if( a[i] % j ==0) } if(a[i]==j || a[i]==1) } }

break; printf("%d\t",a[i]);

5. Write a C program to find whether the number is Armstrong or not ? Ans: A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself.
#include <stdio.h> main() { int number, sum = 0, temp, remainder; printf("Enter a number\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10; sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number."); else printf("Entered number is not an armstrong number."); return 0; }

6. Write a C program to print reverse of a number?


Ans: #include <stdio.h> main() { int n, reverse = 0; printf("Enter a number to reverse\n"); scanf("%d",&n); while (n != 0) { reverse = reverse * 10;

reverse = reverse + n%10; n = n/10; } printf("Reverse of entered number is = %d\n", reverse); return 0; } 7. Write a C program to print the result of student by setting the grade (take the average six subjects and get the percentage print the grade) ?

Prepare a Current bill by verifying the following hints. From 1 to 100 units the unit charges 1 rupee From 101 to 200 units the unit charges 2 rupees From 201 to 350 unit charges 3.50 rupees. If u enters the units output should display the amount.

8.

9. Write a C program if u lower case letter output should be upper case letter ? Ans: - 0x20 for every lower case or add 0x20 for every uppercase to get lower case. 10. Write a C program to print the Pascal triangle.

A Pascal's triangle is a geometric arrangement of the binomial coefficients in a triangle. 1. The rows of Pascal's triangle are conventionally enumerated starting with row 0 which is having only the number 1. 2. For other rows, add the number directly above and to the left with the number directly above and to the right to find the new value. 3. If either the number to the right or left is not present, substitute a zero in its place. For example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 2 in the second row are added to produce the number 3 in the third row. In this way, the rows of the triangle go on infinitely. Here we prompt the user to enter the number of rows to display therefore only five rows will get displayed in the Pascal's triangle.
Ans:

Sample output: Enter the no. of lines: 8 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 #include<stdio.h> long fact(int); int main(){

int line,i,j; printf("Enter the no. of lines: "); scanf("%d",&line); for(i=0;i<line;i++) { for(j=0;j<line-i-1;j++) printf(" "); for(j=0;j<=i;j++) printf("%ld ",fact(i)/(fact(j)*fact(i-j))); printf("\n"); } return 0; } long fact(int num){ long f=1; int i=1; while(i<=num){ f=f*i; i++; } return f; }
11. Write a C program to calculating of average of the numbers ?
Ans:
#include<stdio.h> #include<conio.h> #define N 10 void main() { clrscr(); int count; float sum,average,number; sum=0; count=0; while(count<N) { scanf("%f",&number); sum=sum+number; count=count+1; } average=sum/N; printf("N=%d Sum=%f",N,sum); printf(" Average=%f",average); getch();

/*

SYMBOLIC CONSTANT*/

12. Write a C program to print the tables 0 to 10.?


Ans: int i = 1;

int j = 1; int result = 0 for (i = 1; i <= 12; i++) { for(j = 1; j <= 12; j++) { result = i * j; printf("\n%d X %d = %d", i, j, result); } }

13. Write a switch case by using the functions for adding, subtracting and multiply.? 14. Write a C program to convert decimal to binary.?
Ans: #include<stdio.h>

int main(){ long int binaryNumber,decimalNumber=0,j=1,remainder; printf("Enter any number any binary number: "); scanf("%ld",&binaryNumber); while(binaryNumber!=0) { remainder=binaryNumber%10; decimalNumber=decimalNumber+remainder*j; j=j*2; binaryNumber=binaryNumber/10; } printf("Equivalent decimal value: %ld",decimalNumber); return 0; } Sample output: Enter any number any binary number: 1101 Equivalent decimal value: 13

15. Write a C program to print the years, months, days between the two dates.?
Ans: Program to compute difference between two dates #include<stdio.h>

#include<math.h> void main() { int day1,mon1,year1,day2,mon2,year2; int ref,dd1,dd2,i; clrscr(); printf("Enter first day, month, year"); scanf("%d%d%d",&day1,&mon1,&year1); scanf("%d%d%d",&day2,&mon2,&year2); ref = year1; if(year2<year1) ref = year2; dd1=0; dd1=func1(mon1); for(i=ref;i<year1;i++) { if(i%4==0) dd1+=1; } dd1=dd1+day1+(year1-ref)*365; printf("No. of days of first date fronm the Jan 1 %d= %d",year1,dd1); /* Count for additional days due to leap years*/ dd2=0; for(i=ref;i<year2;i++) { if(i%4==0) dd2+=1; } dd2=func1(mon2)+dd2+day2+((year2-ref)*365); printf("No. of days from the reference year's first Jan = %d",dd2); printf("Therefore, diff between the two dates is %d",abs(dd2-dd1)); getch(); }

int func1(x) //x for month y for dd { int y=0; switch(x) { case 1: y=0; break; case 2: y=31; break; case 3: y=59; break; case 4: y=90; break; case 5: y=120;break; case 6: y=151; break; case 7: y=181; break; case 8: y=212; break; case 9: y=243; break; case 10:y=273; break; case 11:y=304; break; case 12:y=334; break; default: printf("Error encountered"); exit(1); }

return(y); } 16. Write a C program to find the factorial of the given number? Ans:

#include<stdio.h> int main(){ int i=1,f=1,num; printf("Enter a number: "); scanf("%d",&num); while(i<=num){ f=f*i; i++; } printf("Factorial of %d is: %d",num,f); return 0; }
17. Write a C program to convert days into months and days.?
Ans: #include<stdio.h>
#include<conio.h> main() { int days,month,week; clrscr(); printf("enter days"); scanf("%d" ,&days); month=days/30; days%=30; week=days/7; days%=7; printf("equivalent months=%d weeks=%d and leftoverdays=%d" ,month,week,days); getch(); }

18. Write a C program to print the maximum number from the array of given numbers ?
#include<stdio.h> #include<conio.h> void main() { int a[5],max,i; clrscr(); printf(enter elements for the array\n); for(i=0;i<5;i++) scanf(%d,&a[i]); max=a[0]; for(i=1;i<5;i++) { if(max<a[i]) max=a[i]; } printf(the maximum value is%d,max);

getch(); } 19. Write a C program to sort out in the descending order from the array of given numbers.

Ans:
#include <stdio.h> void main () { int i,j,a,n,number[30]; printf ("Enter the value of N\n"); scanf ("%d", &n); printf ("Enter the numbers \n"); for (i=0; i<n; ++i) scanf ("%d",&number[i]); for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j) { if (number[i] < number[j]) { a= number[i]; number[i] = number[j]; number[j] = a; } } } printf ("The numbers arrenged in descending order are given below\n"); for (i=0; i<n; ++i) printf ("%d\n",number[i]); }

20. By using strings compare the two strings and print the biggest value name ?
Ans:

program to compare two strings using strcmp


#include<stdio.h> #include<string.h> main() { char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b);

if( strcmp(a,b) == 0 ) printf("Entered strings are equal.\n"); else printf("Entered strings are not equal.\n"); return 0; }

C program to compare two strings without using strcmp


Here we create our own function to compare strings. int compare(char a[], char b[]) { int c = 0; while( a[c] == b[c] ) { if( a[c] == '\0' || b[c] == '\0' ) break; c++; } if( a[c] == '\0' && b[c] == '\0' ) return 0; else return -1; }

C program to compare two strings using pointers


In this method we will make our own function to perform string comparison, we will use character pointers in our function to manipulate string. #include<stdio.h> int compare_string(char*, char*); main() { char first[100], second[100], result; printf("Enter first string\n"); gets(first); printf("Enter second string\n"); gets(second); result = compare_string(first, second); if ( result == 0 ) printf("Both strings are same.\n"); else printf("Entered strings are not equal.\n");

return 0; } int compare_string(char *first, char *second) { while(*first==*second) { if ( *first == '\0' || *second == '\0' ) break; first++; second++; } if( *first == '\0' && *second == '\0' ) return 0; else return -1;

21. Write a C program to find the string length ?


Ans: #include<stdio.h>

#include<string.h> int string_length(char s[]); main() { char s[50]; int l; printf("Enter the string\n"); scanf("%s\n",s); l=strlen(s); printf("Length of string = %d\n",l); } int string_length(char s[]) { int i; i=0; while(s[i] != '\0') ++i; return i; }

22. By using STRCAT, STRCOMP and STRCPY. Give some example C programs related to it. ? 23. Write a C program to arrange the characters of a string in alphabetical order?
Ans:

#include <stdlib.h> #include<string.h> #include<stdio.h> int main (void)

{ char string[128], temp; int n, i, j; printf("\nEnter string: "); gets(string); n = strlen(string); for (i=0; i<n-1; i++) { for (j=i+1; j<n; j++) { if (string[i] > string[j]) { temp = string[i]; string[i] = string[j]; string[j] = temp; } } } printf("\n%s", string); printf("\n"); return 0; }

24. Write a C program to reverse the string ? Ans: #include<stdio.h>

#include<string.h> int main(){ char str[50]; char *rev; printf("Enter any string : "); scanf("%s",str); rev = strrev(str); printf("Reverse string is : %s",rev); return 0; }
===============2======

C program to reverse a string using pointers #include<stdio.h> int main(){ char str[50];

char rev[50]; char *sptr = str; char *rptr = rev; int i=-1; printf("Enter any string : "); scanf("%s",str); while(*sptr){ sptr++; i++; } while(i>=0){ sptr--; *rptr = *sptr; rptr++; --i; } *rptr='\0'; printf("Reverse of string is : %s",rev); return 0; }

25. Write a C program to reverse the words of the string ? Ans:

#include<stdio.h> int string_length(char*); void reverse(char*); main() { char string[100]; printf("Enter a string\n"); gets(string); reverse(string); printf("Reverse of entered string is \"%s\".\n", string);

return 0; } void reverse(char *string) { int length, c; char *begin, *end, temp; length = string_length(string); begin = string; end = string; for ( c = 0 ; c < ( length - 1 ) ; c++ ) end++; for ( c = 0 ; c < length/2 ; c++ ) { temp = *end; *end = *begin; *begin = temp; begin++; end--; } } int string_length(char *pointer) { int c = 0; while( *(pointer+c) != '\0' ) c++; } return c;

C program to reverse a string using recursion


#include<stdio.h> #include<string.h> void reverse(char*,int,int); main() { char a[100]; gets(a); reverse(a, 0, strlen(a)-1); printf("%s\n",a);

return 0; } void reverse(char *x, int beg, int end) { char a, b, c; if ( beg >= end ) return; c = *(x+beg); *(x+beg) = *(x+end); *(x+end) = c; reverse(x, ++beg, --end); }
========2=====with out string================== /*c program for reverse all words in string*/ #include<stdio.h> #include<conio.h> #include<string.h> int main() { char str[100]; int i,temp; printf("Enter any string : "); gets(str); for(i=0; str[i]!=NULL; i++) { if(str[i+1]==' ' || str[i+1]==NULL) { for(temp=i; temp>=0 && str[temp]!=' '; temp--) printf("%c", str[temp]); } printf(" "); } getch(); return 0; } 26. Write a C program to give the occurrence of a character in a string ?

Ans: In C
const char* str; // the string we want to search

const char c; // the character we want to search for int num = 0; // the number of times c is in str // loop vars int i; const int end = strlen(str); for(i = 0; i < end; ++i) {

if( str[i] == c ) { ++num; }


}

27. Write a C program to addition of matrix ? Ans: #include<stdio.h>

int main(){ int a[3][3],b[3][3],c[3][3],i,j; printf("Enter the First matrix->"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter the Second matrix->"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); printf("\nThe First matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",a[i][j]); } printf("\nThe Second matrix is\n"); for(i=0;i<3;i++){ printf("\n"); for(j=0;j<3;j++) printf("%d\t",b[i][j]); } for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("\nThe Addition of two matrix is\n"); for(i=0;i<3;i++){

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


28. Write a C program to multiply the two matrices using functions ? 29. Write a C program to transpose and inverse of a matrix using functions. ?
Ans: # include<stdio.h>
void display(int [][3]); void main() { int c; void func1(); void func2(); void func3(); void func4(); void func5(); clrscr(); printf("\n- : Matrix Manipulation Functions (for 3 X 3 Matrix) : -"); printf("\n-------------------------------------"); printf("\n Matrix Addition : 1"); printf("\n Matrix Subtraction : 2"); printf("\n Matrix Multiplication : 3"); printf("\n Find Transpose Matrix : 4"); printf("\n Matrix is Symmetric or not : 6"); printf("\n Enter Your Choice : "); scanf("%d",&c); switch(c) { case 1: func1(); break; case 2: func2(); break; case 3: func3(); break; case 4: func4(); break; case 5: func5(); break; default: printf("\nInvalid Choice"); } getch(); }

void func1() { int x[3][3],y[3][3],z[3][3]; void getmatrix(int [][3]); void addition(int [][3],int [][3],int [][3]); clrscr();

getmatrix(x); getmatrix(y); addition(x,y,z); printf("\n - : Matrix 1: - \n"); display(x); printf("\n - : Matrix 2: - \n"); display(y); printf("\n - : Matrix Addition (Result): - \n"); display(z); } void getmatrix(int t[][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter element [%d][%d] : ",i,j); scanf("%d",&t[i][j]); } } } void addition(int p[][3],int q[][3],int r[][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) r[i][j]=p[i][j]+q[i][j]; } } void func2() { int x[3][3],y[3][3],z[3][3]; void getmatrix(int [][3]); void subtraction(int [][3],int [][3],int [][3]); clrscr(); getmatrix(x); getmatrix(y); subtraction(x,y,z); printf("\n - : Matrix 1: - \n"); display(x); printf("\n - : Matrix 2: - \n"); display(y); printf("\n - : Matrix Subtraction (Result): - \n"); display(z); } void subtraction(int p[3][3],int q[3][3],int r[3][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) r[i][j]=p[i][j]-q[i][j]; } } void func3() { int x[3][3],y[3][3],z[3][3]; void getmatrix(int [][3]); void multiplication(int [][3],int [][3],int [][3]); clrscr(); getmatrix(x); getmatrix(y); multiplication(x,y,z); printf("\n - : Matrix 1: - \n"); display(x); printf("\n - : Matrix 2: - \n"); display(y); printf("\n - : Matrix Multiplication (Result): - \n");

display(z); } void multiplication(int p[][3],int q[3][3],int r[3][3]) { int i,j,k; for(i=0;i<3;i++) //condition i< total row of matrix1 { for(j=0;j<3;j++) //condition i< total col of matrix1 or//condition i< total row of matrix2 { r[i][j]=0; for(k=0;k<3;k++) //condition i< total col of matrix2 r[i][j]=r[i][j]+(p[i][j]*q[j][k]); } } } void func4() { int x[3][3],y[3][3]; void getmatrix(int [][3]); void transpose(int [][3],int [][3]); clrscr(); getmatrix(x); transpose(x,y); printf("\n - : Matrix 1: - \n"); display(x); printf("\n - : Transpose Matrix : - \n"); display(y); } void transpose(int p[][3],int q[][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) q[i][j]=p[j][i]; } } void func5() { int x[3][3],y[3][3]; void getmatrix(int [][3]); void transpose(int [][3],int [][3]); int symmetric(int [][3],int [][3]); clrscr(); getmatrix(x); transpose(x,y); if(symmetric(x,y)==1) printf("\nMatrix is Symmetric"); else printf("\nMatrix is Not Symmetric"); } int symmetric(int p[][3],int q[][3]) { int i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { if(q[i][j]!=p[i][j]) return 0; } } return 1; } void display(int m[][3]) {

int i,j; printf("\n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d ",m[i][j]); printf("\n"); } }

==inverse of the matrices================= #include<stdio.h> #include<math.h> float detrm(float[][],float); void cofact(float[][],float); void trans(float[][],float[][],float); main() { float a[25][25],k,d; int i,j; printf("ENTER THE ORDER OF THE MATRIX:\n"); scanf("%f",&k); printf("ENTER THE ELEMENTS OF THE MATRIX:\n"); for(i=0;i<k;i++) { for(j=0;j<k;j++) { scanf("%f",&a[i][j]); } } d=detrm(a,k); printf("THE DETERMINANT IS=%f",d); if(d==0) printf("\nMATRIX IS NOT INVERSIBLE\n"); else cofact(a,k); } /********FUNCTION TO FIND THE DETERMINANT OF THE MATRIX************************/

float detrm(float a[25][25],float k) { float s=1,det=0,b[25][25]; int i,j,m,n,c; if(k==1) { return(a[0][0]); } else { det=0; for(c=0;c<k;c++)

{ m=0; n=0; for(i=0;i<k;i++) { for(j=0;j<k;j++) { b[i][j]=0; if(i!=0&&j!=c) { b[m][n]=a[i][j]; if(n<(k-2)) n++; else { n=0; m++; } } } } det=det+s*(a[0][c]*detrm(b,k-1)); s=-1*s; } } return(det); } =====transpose ===== rogram to find the Transpose of a Matrix #include<stdio.h> #include<conio.h> void main() { int i,j,n,t; int m[5][5]; clrscr(); printf("Enter Order of Matrix : "); scanf("%d",&n); printf("Enter Elements of Matrix :\n\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&m[i][j]); } } printf("Transpose of the Matrix :\n\n"); for(i=0;i<n;i++) {

for(j=i+1;j<n;j++) { t=m[i][j]; m[i][j]=m[j][i]; m[j][i]=t; } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("\t%d",m[i][j]); } printf("\n"); } getch(); }

Output: Enter Order of Matrix : 3 Enter Elements of Matrix : 45 56 96 75 36 15 29 64 81 Transpose of the Matrix : 45 75 29 56 36 64 96 15 81 with out using temp variable: #include<stdio.h> #include<conio.h> void main() { int i,j,n,t; int m[5][5]; clrscr(); printf("Enter Order of Matrix : "); scanf("%d",&n); printf("Enter Elements of Matrix :\n\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { scanf("%d",&m[i][j]); } }

printf("Transpose of the Matrix :\n\n"); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { m[j][i]=(m[i][j]+m[j][i]-(m[i][j]=m[j][i])); } } for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("\t%d",m[i][j]); } printf("\n"); } getch(); }

Write a c program to delete the all consonants from given string. 4. Write a c program to count the different types of characters in given string. 5. Write a c program to sort the characters of a string. 6. Write a c program for concatenation two strings without using string.h header file. 7. Write a c program to find the length of a string using pointer. 8. Write a c program which prints initial of any name.

9. Write a c program to print the string from given character. 12. String concatenation in c without using strcat

Arithmetic operation with pointer in c programming Rule 1: Addition arithmetic with pointers Address + Number= Address Address - Number= Address Address++ = Address Address-- = Address ++Address = Address --Address = Address If we will add or subtract a number from an address result will also be an address. New address will be:

(1)What will be output of following c program? #include<stdio.h> int main(){ int *ptr=( int *)1000; ptr=ptr+1; printf(" %u",ptr); return 0; } Output: 1002

(2)What will be output of following c program? #include<stdio.h> int main(){ double *p=(double *)1000; p=p+3; printf(" %u",p); return 0; } Output: 1024 (3)What will be output of following c program? #include<stdio.h> int main(){ float array[5]={1.1f,2.2f,3.3f}; float(*ptr)[5]; ptr=&array; printf("%u",ptr); ptr=ptr+1; printf(" %u",ptr); return 0; } Output: 1000 1020 (4)What will be output of following c program? #include<stdio.h> typedef struct abc{ int far*a; double b; unsigned char c; }ABC; int main(){ ABC *ptr=(ABC *)1000; ptr=ptr+2; printf(" %u",ptr); return 0; }

Output: 1026

(5)What will be output of following c program? #include<stdio.h> typedef union abc{ char near*a; long double d; unsigned int i; }ABC; int main(){ ABC *ptr=(ABC *)1000; ptr=ptr-4; printf(" %u",ptr); return 0; } Output: 960 (6)What will be output of following c program? #include<stdio.h> float * display(int,int); int max=5; int main(){ float *(*ptr)(int,int); ptr=display; (*ptr)(2,2); printf("%u",ptr); ptr=ptr+1; printf(" %u",ptr); return 0; } float * display(int x,int y){ float f; f=x+y+max; return &f; }

Output: Compiler error Rule 2: Difference arithmetic with pointers Address - Address=Number If you will subtract two pointers result will be a number but number will not simple mathematical subtraction of two addresses but it follow following rule: If two pointers are of same type then:

Consider following example: #include<stdio.h> int main(){ int *p=(int *)1000; int *temp; temp=p; p=p+2; printf("%u %u\n",temp,p); printf("difference= %d",p-temp); return 0; } Output: 1000 1004 Difference= 2 Explanation: Here two pointer p and temp are of same type and both are pointing to int data type varaible. p-temp = (1004-1000)/sizeof(int) =4/2 =2 (1)What will be output of following c program? #include<stdio.h> int main(){ float *p=(float *)1000;

float *q=(float *)2000; printf("Difference= %d",q-p); return 0; } Output: Difference= 250 Explanation: q-p=(2000-100)/sizeof(float) =1000/4 =250 (2)What will be output of following c program? #include<stdio.h> struct abc{ signed char c; short int i; long double l; }; int main(){ struct abc *p,*q; p=(struct abc *)1000; q=(struct abc *)2000; printf("Difference= %d",q-p); return 0; } Output: Difference= 76 Explanation: q-p=(2000-1000)/sizeof(struct abc)= 1000/(1+2+10)=1000/13 =76 //=================================================== (3)What will be output of following c program? #include<stdio.h> typedef union xxx{ char far * c; const volatile i; long int l; }XXX; int main(){ XXX *p,*q;

p=(XXX *)1000; q=(XXX *)2000; printf("Difference= %d",q-p); return 0; } Output: Difference= 250 Explanation: q-p=(2000-1000)/max(4,2,4) =1000/4 =250

(4)What will be output of following c program? #include<stdio.h> int main(){ const volatile array[4]={0}; const volatile(*p)[4]=&array; const volatile(*q)[4]=&array; p++; q++; printf("%u %u\n",p,q); printf("Difference= %d",q-p); return 0; } Output: 1000 1016 (assume) Difference= 2 Explanation: q-p=(1016-1000)/sizeof(const volatile) = 16/ (2*4) =2 Rule 3: Illegal arithmetic with pointers Address + Address=Illegal Address * Address=Illegal Address / Address=Illegal Address % Address=Illegal What will be output of following c program? #include<stdio.h> int main(){ int i=5;

int *p=&i; int *q=(int *)2; printf("%d",p+q); return 0; } Output: Compiler error //======================================================== Rule 4: We can use relation operator and condition operator between two pointers. a. If two pointers are near pointer it will compare only its offset address. What will be output of following c program? #include<stdio.h> int main(){ int near*p=(int near*)0x0A0005555; int near*q=(int near*)0x0A2115555; if(p==q) printf("Equql"); else printf("Not equal"); return 0; } Output: Equal //========================================================= b. If two pointers are far pointer it will compare both offset and segment address. What will be output of following c program? #include<stdio.h> int main(){ int far*p=(int far*)0x0A0005555; int far*q=(int far*)0x0A2115555; if(p==q) printf("Equql");

else printf("Not equal"); return 0; } Output: Not equal //========================================================= c. If two pointers are huge pointer it will first normalize into the 20 bit actual physical address and compare to its physical address.

What will be output of following c program? #include<stdio.h> int main(){ int huge*p=(int huge*)0x0A0005555; int huge*q=(int huge*)0x0A2113445; if(p==q) printf("Equql"); else printf("Not equal"); return 0; } Output: Equal Rule 5: Bit wise arithmetic with pointers We can perform bit wise operation between two pointers like Address & Address=Illegal Address | Address=Illegal Address ^ Address=Illegal ~Address=Illegal What will be output of following c program? #include<stdio.h> int main() { int i=5,j=10; int *p=&i;

int *q=&j; printf("%d",p|q); return 0; } Output: Compiler error //======================================================== Rule 6: We can find size of a pointer using sizeof operator. What will be output of following c program? #include<stdio.h> int main(){ int near*far*huge* p; printf("%d",sizeof(p)); printf(" %d",sizeof(*p)); printf(" %d",sizeof(**p)); return 0; } Output: 4 4 2 //=====================================================================