simple C Example program
#include<stdio.h> main() { int a=10,b=20,c=30; printf("a=%d b=%d c=%d\n",a,b,c); a=++b,b=c++,b+c; printf("a=%d b=%d c=%d\n",a,b,c); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex32.c madan@madan-Lenovo-G570:~/madan$ ./a.out a=10 b=20 c=30 a=21 b=30 c=31program to check arithmatic operations
#include<stdio.h> int sum(int,int); int sub(int,int); int mul(int,int); int div(int,int); int a=34,b=32; int d,s,k,m; main() { printf("inside main(): a and b are %d\t%d\n",a,b); s=sum(a,b); k=sub(a,b); m=mul(a,b); d=div(a,b); } int sum(int a,int b) { s=(a+b); printf("sum of two numbers is %d\n",s); } int sub(int a,int b) { k=(a-b); printf("substraction of two numbers is %d\n",k); } int mul(int a,int b) { m=(a*b); printf("multiplication of numbers is%d:\n",m); } int div(int a,int b) { d=(a/b); printf("division of two numbers is %d\n",d); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex33.c madan@madan-Lenovo-G570:~/madan$ ./a.out inside main(): a and b are 34 32 sum of two numbers is 66 substraction of two numbers is 2 multiplication of numbers is1088: division of two numbers is 1program to print ASCII equalent symbols
#include<stdio.h> #include<stdlib.h> main() { int num,bit; char ch; printf("program to print ascii values of symbols\n"); num=0; INPUT: printf("%d %c \n",num,num); bit=7; TEST: num&(1<<bit)?printf("1"):printf("0"); bit--; if(bit>=0) goto TEST; printf("\t"); num++; if(num<=127) goto INPUT; }OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out program to print ascii values of symbols 0 00000000 1 00000001 2 00000010 3 00000011 4 00000100 5 00000101 6 00000110 7 00000111 8 00001000 9 00001001 10 00001010 11 00001011 12 0001100 13 00001101 14 00001110 15 00001111 16 00010000 17 00010001 18 00010010 19 00010011 20 00010100 21 00010101 22 00010110 23 00010111 24 00011000 25 00011001 26 00011010 27 00011011 28 00011100 29 00011101 30 00011110 31 ¬ 00011111 32 00100000 33 ! 00100001 34 " 00100010 35 # 00100011 36 $ 00100100 37 % 00100101 38 & 00100110 39 ' 00100111 40 ( 00101000 41 ) 00101001 42 * 00101010 43 + 00101011 44 , 00101100 45 - 00101101 46 . 00101110 47 / 00101111 48 0 00110000 49 1 00110001 50 2 00110010 51 3 00110011 52 4 00110100 53 5 00110101 54 6 00110110 55 7 00110111 56 8 00111000 57 9 00111001 58 : 00111010 59 ; 00111011 60 < 00111100 61 = 00111101 62 > 00111110 63 ? 00111111 64 @ 01000000 65 A 01000001 66 B 01000010 67 C 01000011 68 D 01000100 69 E 01000101 70 F 01000110 71 G 01000111 72 H 01001000 73 I 01001001 74 J 01001010 75 K 01001011 76 L 01001100 77 M 01001101 78 N 01001110 79 O 01001111 80 P 01010000 81 Q 01010001 82 R 01010010 83 S 01010011 84 T 01010100 85 U 01010101 86 V 01010110 87 W 01010111 88 X 01011000 89 Y 01011001 90 Z 01011010 91 [ 01011011 92 \ 01011100 93 ] 01011101 94 ^ 01011110 95 _ 01011111 96 ` 01100000 97 a 01100001 98 b 01100010 99 c 01100011 100 d 01100100 101 e 01100101 102 f 01100110 103 g 01100111 104 h 01101000 105 i 01101001 106 j 01101010 107 k 01101011 108 l 01101100 109 m 01101101 110 n 01101110 111 o 01101111 112 p 01110000 113 q 01110001 114 r 01110010 115 s 01110011 116 t 01110100 117 u 01110101 118 v 01110110 119 w 01110111 120 x 01111000 121 y 01111001 122 z 01111010 123 { 01111011 124 | 01111100 125 } 01111101 126 ~ 01111110 127 Program to check operator results
#include<stdio.h> #include<stdlib.h> operators() { printf("to check operator results\n"); implicit(); assign(); arithmatic(); exit(0); } implicit() { float a; float b; int c; printf("enter values for a and b\n"); scanf("%f%f",&a,&b); c=a/(int)b; printf("division is %d\n",c); } assign() { int a,b,c,d,e,f; printf("enter values for a and b\n"); scanf("%d%d",&a,&b); c=(a=b); d=a; e=(c>d); f=(d!=e); printf("c=%d\td=%d\te=%d\tf=%d\n",c,d,e,f); } arithmatic() { int a,b,g; float c,d,e,f; printf("enter values for a and b\n"); scanf("%d%d",&a,&b); c=a+b; d=a-b; e=a*b; f=a/b; g=a%b; printf("c=%f\td=%f\te=%f\tf=%f\tg=%d\n",c,d,e,f,g); exit(0); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles ex36.c /usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400440 madan@madan-Lenovo-G570:~/madan$ ./a.out to check operator results enter values for a and b 12 2 division is 6 enter values for a and b 12 2 c=2 d=2 e=0 f=1 enter values for a and b 12 2 Segmentation fault (core dumped)Example program for floating point values
#include<stdio.h> #include<stdlib.h> main() { int a=100,b=20; float c,c1,c2,c3; c=a/b; printf("%f\n",c); c1=(float)a/(float)b; printf("%f\n",c1); c2=(float)a/b; printf("%f\n",c2); c3=a/(float)b; printf("%f\n",c3); printf("%d\t%d\n",a,b); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex37.c madan@madan-Lenovo-G570:~/madan$ ./a.out 5.000000 5.000000 5.000000 5.000000 100 20Example program to view the difference between implicit & Explicit declarations
#include<stdio.h> #include<stdlib.h> main() { float a=100,b=20; int c,c1; float c2,c3; c=a; c1=b; c2=a; c3=b; printf("%f\t%f\t%f\t%f\n",(float)c,(float)c1,c2,c3); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex38.c ex38.c: In function �main�: ex38.c:11:1: warning: incompatible implicit declaration of built-in function �printf� [enabled by default] printf("%f\t%f\t%f\t%f\n",(float)c,(float)c1,c2,c3); ^ madan@madan-Lenovo-G570:~/madan$ ./a.out 100.000000 20.000000 100.000000 20.000000Program to print integer range
#include<stdio.h> main() { signed short int i; i=32767; printf("i=%hd\n",i); i++; printf("i=%hd\n",i); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex39.c madan@madan-Lenovo-G570:~/madan$ ./a.out i=32767 i=-32768Program for Logical operations checking
#includeOutPut:#include main() { int a=12,b=14,c,c1,c2,c3,c4; c=a&b; printf("%d\n",c); c=a|b; printf("%d\n",c); c=a^b; printf("%d\n",c); c=~a; printf("%d\n",c); c1=1<<1; printf("%d\n",c1); c2=1<<10; printf("%d\n",c2); c3=1<<30; printf("%d\n",c3); c4=1<<34; printf("%d\n",c4); }
madan@madan-Lenovo-G570:~/madan$ cc ex40.c ex40.c: In function ‘main’: ex40.c:20:1: warning: left shift count >= width of type [enabled by default] c4=1<<34; ^ madan@madan-Lenovo-G570:~/madan$ ./a.out 12 14 2 -13 2 1024 1073741824 0Program for Logical operations checking
#include<stdio.h> #include<stdlib.h> main() { int a=-5,b=3,c=0,d; d=(a==b)&&(b==c)&&(c==a); printf("%d\n",d); d=(a=b)||(b=c)||(c=a); printf("%d\n",d); d=(a=b)&&(b=c)||(c=a); printf("%d\n",d); d=(a=b)||(b=c)&&(c=a); printf("%d\n",d); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex41.c madan@madan-Lenovo-G570:~/madan$ ./a.out 0 1 1 0Program to find the grade of student
#include<stdio.h> #include<stdlib.h> main() { int m1,m2,m3,m4,total; float avg; char grade; printf("enter marks in four subjects\n"); scanf("%d%d%d%d",&m1,&m2,&m3,&m4); total=(m1+m2+m3+m4); avg=total/4; printf("total and average marks are %d\t%f\n",total,avg); if(avg>=85) grade='A'; else if(avg>=70) grade='B'; else if(avg>=60) grade='C'; else if(avg>=50) grade='D'; else grade='E'; printf("grade is %c\n",grade); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex42.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter marks in four subjects 43 54 65 76 total and average marks are 238 59.000000 grade is DProgram to check logical operations_1
main() { int choice,num,bit; printf("enter a num and bit\n"); scanf("%d%d",&num,&bit); printf("enter any choice\n"); scanf("%d",&choice); if(choice==1) { printf("to set a particulart bit\n"); num=num|(1<<bit); printf("number is %d \n",num); } else if(choice==2) { printf("to clear a particular bit\n"); num=num&~(1<<bit); printf("number is %d \n",num); //return; } else if(choice==3) { printf("to complement a particular bit\n"); num=num^(1<<bit); printf("number is %d \n",num); } else if(choice==4) { printf("to test a particular bit\n"); num&(1<<bit)?printf("set \n"):printf("clear \n"); } else if(choice==5) { printf("exit\n"); } else { printf("eneter currect choice\n"); } //printf("number is %d",num); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex43.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter a num and bit 34 4 enter any choice 1 to set a particulart bit number is 50Program to check logical operations_2
#include<stdio.h> main() { int choice,num,bit; printf("enter a num and bit\n"); scanf("%d%d",&num,&bit); printf("enter any choice\n"); scanf("%d",&choice); if(choice==1) { printf("to set a particulart bit\n"); num=num|(1<<bit); printf("number is %d",num); } if(choice==2) { printf("to clear a particular bit\n"); num=num&~(1<<bit); printf("number is %d",num); //break; } if(choice==3) { printf("to complement a particular bit\n"); num=num^(1<<bit); printf("number is %d",num); //break; } if(choice==4) { printf("to test a particular bit\n"); num&(1<<bit)?printf("set"):printf("clear"); //break; } if(choice==5) { printf("exit"); //break; } if(choice>5) { printf("eneter currect choice\n"); } //printf("number is %d",num); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex44.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter a num and bit 23 4 enter any choice 2 to clear a particular bit number is 7Program to check logical operations_3(set,clear bits in a particular number)
#include<stdio.h> main() { int choice,num,bit; printf("enter a num and bit\n"); scanf("%d%d",&num,&bit); printf("enter any choice\n"); scanf("%d",&choice); switch(choice) { case 1: printf("set bit\n"); num=num|(1<<bit); printf("number is %d",num); break; case 2: printf("clear bit\n"); num=num&~(1<<bit); printf("number is %d",num); break; case 3: printf("complement bit\n"); num=num^(1<<bit); printf("number is %d",num); break; case 4: printf("test bit\n"); num&(1<<bit)?printf("set"):printf("clear"); break; case 5: printf("exit"); break; default: printf("eneter currect choice\n"); } //printf("number is %d",num); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex45.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter a num and bit 32 4 enter any choice 2 clear bit number is 32program to check addresses using pointers
#include<stdio.h> main() { int a=65,b=67,*var; printf("b=%d at %u\n",b,&b); var=&b; printf("var=%d at %u\n",var); printf("var=%d at %u \n",*var); //printf("a= at %u\n",a,&a); //printf("b=%d at %u\n",b,&b); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex47.c ex47.c: In function �main�: ex47.c:5:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=] printf("b=%d at %u\n",b,&b); ^ ex47.c:7:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �int *� [-Wformat=] printf("var=%d at %u\n",var); ^ ex47.c:7:1: warning: format �%u� expects a matching �unsigned int� argument [-Wformat=] ex47.c:8:1: warning: format �%u� expects a matching �unsigned int� argument [-Wformat=] printf("var=%d at %u \n",*var); ^ madan@madan-Lenovo-G570:~/madan$ ./a.out b=67 at 4180353072 var=-114614224 at 2893998576 var=67 at 2893998576program to find power of a number
#include<stdio.h> main() { int a,b,result=1; printf("enter values for a and b\n"); scanf("%d%d",&a,&b); while(b!=0) { result=result*a; b--; } printf("result is %d\n",result); }Output:
madan@madan-Lenovo-G570:~/madan$ cc ex48.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter values for a and b 3 4 result is 81program to find sum of digits of a number
#include<stdio.h> #include<stdlib.h> main() { int n,sum=0,rem,k; printf("enter value for n\n"); scanf("%d",&n); k=n; for(;n>0;n=n/10) { rem=n%10; sum=sum+rem; } printf("sum of digits of %d is %d\n",k,sum); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex49.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter value for n 5 sum of digits of 5 is 5 madan@madan-Lenovo-G570:~/madan$ ./a.out enter value for n 12345 sum of digits of 12345 is 15program to print sum and product of ODD numbers
#include<stdio.h> #include<stdlib.h> main() { int n,i,sum=0,pro=1; printf("enter value for n\n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(i%2!=0) { printf("odd numbers are %d\n",i); sum=sum+i; pro=pro*i; } } printf("sum of n odd numbers is %d\n",sum); printf("product of n odd numbers is %d\n",pro); }OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out enter value for n 15 odd numbers are 1 odd numbers are 3 odd numbers are 5 odd numbers are 7 odd numbers are 9 odd numbers are 11 odd numbers are 13 odd numbers are 15 sum of n odd numbers is 64 product of n odd numbers is 2027025program to check properties of printf function
#include<stdio.h> main() { int a,b,c; printf("eneter values for a and b\n"); printf("a and b values are",scanf("%d %d",&a,&b)); printf("\n"); c=a+b; printf("c=%d",c); printf("\n"); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex51.c ex51.c: In function �main�: ex51.c:6:1: warning: too many arguments for format [-Wformat-extra-args] printf("a and b values are",scanf("%d %d",&a,&b)); ^ madan@madan-Lenovo-G570:~/madan$ ./a.out eneter values for a and b 10 20 a and b values are c=30Program to print addresses of integers
#include<stdio.h> main() { int ch; int *p=&ch; printf("%d at %u\n",*p,&ch); *p=100; printf("%d at %u\n",*p,&p); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex52.c ex52.c: In function �main�: ex52.c:6:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=] printf("%d at %u\n",*p,&ch); ^ ex52.c:8:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int **� [-Wformat=] printf("%d at %u\n",*p,&p); ^ madan@madan-Lenovo-G570:~/madan$ ./a.out 32767 at 503631812 100 at 503631816program to reverse bits of an integer
#include<stdio.h> main() { int n,i,j,m,k; printf("enter n value\n"); scanf("%d",&n); printf("n=%d\n",n); for(i=31;i>=0;i--) printf("%d",(n>>i)&1?1:0); printf("\n"); for(i=31,j=0;i>j;i--,j++) { m=(n>>i)&1; k=(n>>j)&1; if(m^k) { n=n^(1<<i); n=n^(1<<j); } } printf("n=%d\n",n); for(i=31;i>=0;i--) printf("%d",(n>>i)&1?1:0); printf("\n"); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex53.c madan@madan-Lenovo-G570:~/madan$ ./a.out enter n value 15 n=15 00000000000000000000000000001111 n=-268435456 11110000000000000000000000000000Program to check size of data types
#include<stdio.h> main() { //int a=4,b; //char ch='A'; printf("sizeof integer is %d\n",sizeof(int)); printf("size of float is %d\n",sizeof(float)); printf("size of real type is %d\n",sizeof(double)); printf("size of long real type is %d\n",sizeof(long double)); printf("size of long integer type is %d\n",sizeof(long long int)); printf("size of signed integer type is %d\n",sizeof(signed int)); printf("size of unsigned integer type is %d\n",sizeof(unsigned int)); printf("size of signed char type is %d\n",sizeof(signed char)); printf("size of unsigned char type is %d\n",sizeof(unsigned char)); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex55.c ex55.c: In function �main�: ex55.c:6:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("sizeof integer is %d\n",sizeof(int)); ^ ex55.c:7:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of float is %d\n",sizeof(float)); ^ ex55.c:8:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of real type is %d\n",sizeof(double)); ^ ex55.c:9:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of long real type is %d\n",sizeof(long double)); ^ ex55.c:10:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of long integer type is %d\n",sizeof(long long int)); ^ ex55.c:11:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of signed integer type is %d\n",sizeof(signed int)); ^ ex55.c:12:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of unsigned integer type is %d\n",sizeof(unsigned int)); ^ ex55.c:13:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of signed char type is %d\n",sizeof(signed char)); ^ ex55.c:14:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �long unsigned int� [-Wformat=] printf("size of unsigned char type is %d\n",sizeof(unsigned char)); ^ madan@madan-Lenovo-G570:~/madan$ ./a.out sizeof integer is 4 size of float is 4 size of real type is 8 size of long real type is 16 size of long integer type is 8 size of signed integer type is 4 size of unsigned integer type is 4 size of signed char type is 1 size of unsigned char type is 1Program to swap two numbers using 3 different methods
#include<stdio.h> #include<stdlib.h> main() { int a,b; printf("swap by using XOR\n"); printf("enter values for a and b\n"); scanf("%d%d",&a,&b); a^=b^=a^=b; printf("a=%d\tb=%d\n",a,b); manip(); temp(); } manip() { int a,b; printf("swap 2 numbers by manipulation\n"); printf("enter values for a and b\n"); scanf("%d%d",&a,&b); b=(a*b)/(a=b); printf("a=%d\t b=%d\n",a,b); } temp() { int a,b,temp; printf("swap 2 numbers by Temp Variable\n"); printf("enter values for a and b\n"); scanf("%d%d",&a,&b); temp=a; a=b; b=temp; printf("a=%d\tb=%d\n",a,b); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex56.c madan@madan-Lenovo-G570:~/madan$ ./a.out swap by using XOR enter values for a and b 10 20 a=20 b=10 swap 2 numbers by manipulation enter values for a and b 12 32 a=32 b=12 swap 2 numbers by Temp Variable enter values for a and b 32 54 a=54 b=32Program to print Multiplication Tables
#include<stdio.h> main() { int i=1,j=1,n; printf("multiplication tables are...\n"); for(i=1;i<=10;i++) { for(j=1;j<=10;j++) printf("%d\t",i*j); printf("\n"); } }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex57.c madan@madan-Lenovo-G570:~/madan$ ./a.out multiplication tables are... 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100Cube Root Of a Number
#include<stdio.h> main() { int num,i,r,sum=0,temp; printf("enter any number\n"); scanf("%d",&num); temp=num; while(temp>0) { r=temp%10; sum=sum+(r*r*r); temp=temp/10; } printf("cube root of a number=%d\n",sum); }OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out enter any number 5 cube root of a number=125Sum of integers through command line arguments
#include<stdio.h> main(int argc,char **argv) { int r=0,sum=0,i,a[100]; for(i=1;i<argc;i++) { printf("argument values are %d\n",r=atoi(argv[i])); //r=atoi(argv[i]); sum=r+sum; } //printf("%d %d\n",sizeof(argv),sizeof(argc)); printf("sum=%d\n",sum); }OutPut:
madan@madan-Lenovo-G570:~/madan$ cc storage.c madan@madan-Lenovo-G570:~/madan$ ./a.out sum=0 madan@madan-Lenovo-G570:~/madan$ ./a.out 13 34 45 56 77 argument values are 13 argument values are 34 argument values are 45 argument values are 56 argument values are 77 sum=225