
The general form of a C program is as follows:
Pre-processor directives Global declaration Main() { Local variables to function main; Statements associated with function main(); } Fun1() { Local variables to function fun1(); Statements associates with function fun1(); } Fun2() { Local variables to function fun2(); Statements associates with function fun2(); }
1. A program to print Hello World.
#include<stdio.h> /*main function starts here*/ Void main() { Printf(“Hello world!\n”); }
Output: Hello world
2. Using #define in a Program
#include<stdio.h> #include<conio.h> #define BELL ‘\a’ Void main() { Printf(“Press any key to hear the bell!”); getch(); printf(“%c”, BELL); }
Output: Press any key to hear the bell.
3. Using const in a C program
#include<stdio.h> Void main() { Const float PI=3.142857142857; Float area, perimeter, radius; Printf(“enter the radius:”); Scanf(“%f”, &radius); Area=PI*radius*radius; Perimeter=2*PI*radius; Printf(“Area=%f sq. unit and perimeter=%f unit”, area, perimeter); }
4. Using printf() function in a C program
#include<stdio.h> Main() { Printf(“this is an example of something printed!”); Return 0; }
5. Using scanf() function in a C program
#include<stdio.h> Main() { Float y; Int x; Puts(“enter a float, then an int”); Scanf(“%f%d”, &y, &x); Printf(“\nYou entered %f and %d”, y, x); Return 0; }
6. Another simple program in C using int and printf()
#include<stdio.h> Main() { Int a, b, average; a=10; b=6; average=(a+b)/2; printf(“Here ”); printf(“is ”); printf(“the ”); printf(“answer….. ”); printf(“\n ”); printf(“%d ”, average); }
7. A simple program to Demonstrate scanf() function
#include<stdio.h> Main() { int a, b, c; printf(“\nThe first number is ”); scanf(“%d”, &a); printf(“\nThe second number is ”); scanf(“%d”, &b); c=a+b; printf(“The answer is %d\n”, c); }
8. A while loop and 100 Hello Worlds
#include<stdio.h> Main() { int count; count=0; while(count<100) { ++count; Printf(“Hello World!\n”); } }
9. The do while loop and a 100 Hello Worlds
#include<stdio.h> main() { int count; count=0; do { ++count; Printf(“Hello World!\n”); } While(count<100); }
10. This program demonstrates the for loop
#include<stdio.h> main() { int fahr; for(fahr=0;fahr<=300;fahr=fahr+20) printf(“%4d%6.1f\n”, fahr, (5.0/9.0)*(fahr-32)); }
11. This program demonstrates the while loop
#include<stdio.h> Main() { Int lower, upper, step; Float fahr, Celsius; Lower=0; Upper=300; Step=20; Fahr=lower; While(fahr<=upper) { Celsius=(5.0/9.0)*(fahr-32.0); Printf(“%d.0f%6.1f\n”, fahr, celsius); Fahr=fahr+step; } }
12. This program demonstrates the if statement
#include<stdio.h> Main() { Int a, b; Do { Printf(“\nEnter first number:”); Scanf(“%d”, &a); Printf(“\nEnter second number:”); Scanf(“%d”, &b); If(a<b) printf(“\n\nfirst number is less than second\n\n”); if(b<a) printf(“second number is less than first number\n\n”); } while(a<999); }
13. This program demonstrates the if-block
#include<stdio.h> main() { int a, b; do { printf(“\nEnter first number:”); scanf(“%d”, &a); printf(“\nEnter second number:”); scanf(“%d”, &b); if (a<b){ printf(“\n\nFirst number is less than second\n”); printf(“Their difference is: %d\n”, b-a); printf(“\n”); } printf(“\n”); }while(a<999); }
14. The program demonstrates the Use of the if else block
#include<stdio.h> main() { int num1, num2; printf(“\nEnter the first number”); scanf(“%d”, &num1); printf(“\nEnter the second number”); scanf(“%d”, &num2); if(num2==0) printf(“\n\nCannot divide by zero\n\n”); else printf(“\n\nAnswer is %d\n\n”, num1/num2); }
15. A simple program to show the use of continue statement
#include<stdio.h> main() { int x; for (x=0; x<=100; x++){ if (x%2) continue; printf(“%d\n”, x); } }
16. This program jumps out of an infinite loop using the break statement
#include<stdio.h> main() { int t; for ( ; ; ){ scanf(“%d”, &t); if (t==10) break; } printf(“End of an infinite loop….\n”); }
17. This program demonstrates the switch statement
#include<stdio.h> main() { int i; printf(“Enter a number between 1 and 4”); scanf(“%d”, &i); switch(i) { case 1; printf(“One”); break; case 2; printf(“Two”); break; case 3; printf(“three”); break; case 4; printf(“Four”); break; default; printf(“Unrecognized number”); } /*End of switch*/ }
18. A program in C having No argument with return value
#include<stdio.h> main() { int test(void); int max; max=test(); printf(“The maximum of the two is : %d”, max); } int test(void) { int x, y; printf(“Enter 1st no.:”); scanf(“%d”, &x); printf(“Enter 2nd no.:”); scanf(“%d”, &y); return(x>y?x:y); }
19. A program in C with Argument and with return value
#include<stdio.h> main() { int test(int, int); int x, y, max; printf(“Enter 1st no.:”); scanf(“%d”, &x); printf(“Enter 2nd no.:”); scanf(“%d”, &y); max=test(x,y); printf(“The maximum of the two is %d”, max); } int test(int a, int b) { return(a>b?a:b); }
20. C program having argument with no return value
#include<stdio.h> main() { void test (int, int); int x, y; printf(“Enter 1st no.:”); scanf(“%d”, &x); printf(“Enter 2nd no.:”); scanf(“%d”, &y); test(x,y); } void test(int a, int b) { int max; max=a>b?a:b; printf(“The maximum of the two is: %d”, max); }
21. A program in C using Call by Value
#include<stdio.h> main() { void swap(int, int); int a=10, b=20; swapv(a, b); printf(“\n a=%d b=%d”, a, b); } void swapv(int x, int y) { int t; t=x; x=y; y=t; printf(“\n x=%d y=%d”, x, y); }
22. A Program in C using Call by reference
#include<stdio.h> main() { void swapr(int *, int *); int a=10, b=20; swapr(&a, &b); printf(“\n a=%d b=%d”, a, b); } void swapr(int *x, int *y) { int t; t=*x; *x=*y; *y=t; }
23. Another program in C using Call by reference
#include<stdio.h> main() { void areaperi(int, float *, float *); int radius; float area, perimeter; printf(“Enter radius of a circle:”); scanf(“%d”, &radius); areaperi(radius, &area, &perimeter); printf(“Area=%f”, area); printf(“\nPerimeter=%f”, perimeter); } void areaperi(int r, float *a, float *p) { *a=3.14*r*r; *p=2*3.14*r; }
24. A program in C of Recursive function to calculate the factorial value of a given interger
#include<stdio.h> main() { int rec(int ); int n, fact; printf(“Enter an integer to find its factorial value:”); scanf(“%d”, fact); fact=rec(n); printf(“Factorial value=%d”, fact) } int rec(int x) { int f; if(x==1) return(1); else f=x*rec(x 1); return(f); }
25. A program in C Passing array elements to a function
#include<stdio.h> main() { void display(int ); int i; int a[]={35, 45, 55, 65, 75}; for (i=0; i<5; i++) display(a[i]); } void display(int m) { printf(“%d”, m); }
26. A program in C passing an entire array into a function
#include<stdio.h> main() { void display(int *); int a[]={35, 45, 55, 65, 75}; display(&a[0]); } void display(int *m) { int i; for(i=0; i<5; i++) { printf(“%d”, *m) } }
27. A program in C for double dimension array
#include<stdio.h> #include<conio.h> main() { int a[10][10], i, j, r, c; clrscr(); printf(“Enter order of matrix:”); scanf(“%d%d”, &r, &c); printf(“Enter %d values”, r*c); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf(“%d”, &a[i][j]); printf(“\n Elements of array a:”); for(i=0;i<r;i++) { printf(“%d\t”, a[i][j]); } } getch(); }
28. A program in C for Matrix Addition
#include<stdio.h> #include<conio.h> main() { int a[10][1], b[10][10], d[10][10]; int i, j, r, c, p, q; clrscr(); printf(“Enter order of matrix a:”); scanf(“%d%d”, &r, &c); /*accepting values from user*/ printf(“Enter %d values”, r*c); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf(“%d”, &a[i][j]); printf(“Enter order of matrix b:”); scanf(“%d%d”, &p, &q); /*accepting values from user*/ printf(“Enter %d values”, p*q); for(i=0;i<p;i++) for(j=0;j<q;j++) scanf(“%d”, &a[i][j]); /*printing values*/ printf(“Elements of array a:”); for(i=0;i<r;i++) { printf(“\n”); for(j=0;j<c;j++) { printf(“%d\t”, a[i][j]); } } printf(“Elements of array b:”); for(i=0;i<p;i++) { printf(“\n”); for(j=0;j<q;j++) { printf(“%d\t”, b[i][j]); } } /*if rows and columns of two matrices are equal*/ if(r==p && c==q) { for(i=0;i<r;i++) { for(j=0;j<q;j++) d[i][j]=a[i][j]+b[i][j]; } printf(“\nElements of array d:”); for(i=0;i<p;i++) { printf(“\n”); for(j=0;j<q;j++) { printf(“%d\t”, d[i][j]); } } } else printf(“\nAddition is not possible”); getch(); }
29. A program in C to obtain the memory address of a variable
#include<stdio.h> main() { int i=5; int *j; /*here we are declaring the pointer variable*/ j=&i; /*assigning the memory address of i into j*/ printf(“\n Address of i=%u”, &i); printf(“\n Address of j=%u”, &j); printf(“\n Value of j=%u”, j); printf(“\n Value of i=%u”, i); printf(“\n Value of i=%u”, * j); }
30. A program in C to demonstrate pointer use
#include<stdio.h> /*Declare and initialize an int variable*/ /*Declare a pointer to int*/ int *ptr; main() { /*initialize ptr to pointer to var*/ ptr=&var; /*Access var directly and indirectly*/ printf(“\nDirect access, var=%d”, var); printf(“\n Indirect access, var=%d”, *ptr); /*Display the address of var two ways*/ printf(“\n\nThe address of var=%d”, &var); printf(“\nThe address of var=%d”, ptr); return 0; }
31. A program in C to demonstrate pointer to pointer concept
#include<stdio.h> #include<conio.h> main() { int i=5; int *j; int **k; j=&I; k=&j; printf(“\n Address of i=%u”, &i); printf(“\n Address of i=%u”, j); printf(“\n Address of i=%u”, *k); printf(“\n Address of j=%u”, &j); printf(“\n Address of j=%u”, k); printf(“\n Value of j=%u”, j); printf(“\n Value of k=%u”, k); printf(“\n Value of i=%d”, i); printf(“\n Value of i=%d”, *j); printf(“\n Value of i=%u”, **k); }
32. A program in C for pointer with Arrays
#include<stdio.h> #include<conio.h> main() { int a[20], d, I; int *ptr; clrscr(); printf(“enter dimension:”); scanf(“%d”, &d); printf(“\nEnter %d values:”, d); for (I=0; I<d:I++) scanf(“%d”, &a[I]); ptr=a; printf(“\nElements of array a:\n”); for(I=0;I<d;I++) { printf(“%d\t”, *ptr); ptr++; } getch(); }
33. A program in C to demonstrate pointer and multidimensional arrays
#include<stdio.h> #include<conio.h> int multi[2][4]; main() { printf(“\nmulti=%u”, multi); printf(“\nmulti[0]=%u”, multi[0]); printf(“&multi[0][0]=%u\n”, &multi[0][0]); return 0; }
34. Another simple program in C, using pointers
#include<stdio.h> #include<conio.h> main() { float a=10; float *points_to_a=&a; printf(“The value in a:%f\n”, a); printf(“The value pointed to by points_to_a:%f\n”, *points_to_a); printf(“The value of points_to_a in hex:%x\n”, points_to_a); printf(“The value of points_to_a as an integer:%d\n”, points_to_a); printf(“The value of points_to_a as a float:%f\n”, points_to_a); *points_to_a=-10; printf(“The value pointed to by points_to_a:%f\n”, *points_to_a); printf(“The value of points_to_a in hex:%x\n”, points_to_a); printf(“The value of points_to_a as an integer:%d\n”, points_to_a); printf(“The value of points_to_a as a float:%f\n”, points_to_a); exit(0); }