Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, 9 April 2014

C Programming Important Links

Read More

Sunday, 6 April 2014

Example Programs on OS Concepts

myHeader.h
#include<stdio.h>
#include<stdlib.h>
#include<sys/resource.h>
#include<utime.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>
#include<time.h>
#include<sys/resource.h>
#include<unistd.h>
#include<utime.h>
#include<fcntl.h>
#include<sys/timeb.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<limits.h>
#include<dirent.h>
#include<errno.h>
#include<stdio.h>
#include<sys/sem.h>
#include<sys/ipc.h>
#include<sys/time.h>
#include<sys/timeb.h>
stat to find size of our file
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdio.h>
main(int argc,char **argv)
{
struct stat buf;
if(stat(argv[1],&buf)==-1)
{
perror("stat");
return;
}
printf("size=%u\n",(unsigned int)buf.st_size);
printf("mode=%u\n",(unsigned int)buf.st_size);
}
file name:madan
welcome madan reddy
hyderabad
always truth wins
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
size=48
mode=48
program to find resource limits
#include"myHeader.h"
main()
{
struct rlimit v,new;
getrlimit(RLIMIT_STACK,&v);
new=v;
new.rlim_cur=2000;
setrlimit(RLIMIT_STACK,&new);
getrlimit(RLIMIT_STACK,&new);
printf("new resource current limit address=%lu \n new resource maximum limit=%lu\n",new.rlim_cur,new.rlim_max);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex2.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
new resource current limit address=2000 
 new resource maximum limit=18446744073709551615
fork system call for dual processes
#include"myHeader.h"
main()
{
fork(); fork(); fork();
printf("pid:%d ppid:%d \n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex3.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
pid:3351 ppid:2817 
pid:3353 ppid:3351 
pid:3356 ppid:3353 
pid:3352 ppid:3351 
pid:3354 ppid:1144 
pid:3357 ppid:1144 
pid:3355 ppid:1144 
pid:3358 ppid:1144
pipe for read and write operations
#include"myHeader.h"
char ch='a';
main()
{
int p[2];
pipe(p);
if(fork()==0)
{
printf("%d ",getpid());
read(p[0],&ch,1);
printf("%c\n ",ch);
//ch++;
}
else
{
printf("%d ",getppid());
for(ch='a';ch<='z';ch++)
write(p[1],&ch,1);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex4.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
2817 3378 a
parent & child relation program
#include"myHeader.h"
int main()
{
if(fork())
{
if(fork())
{
if(fork())
printf("parent id :%d\n",getpid());
else
printf("child3 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child2 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child1 id: %d parent id: %d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child1 id: 3514 parent id: 3513
parent id :3513
child2 id: 3515 parent id:3513
child3 id: 3516 parent id:1144
program with 3forks(3child,3parents)
#include"myHeader.h"
int main()
{
if(fork())
{
printf("child1 id: %d parent1 id: %d\n",getpid(),getppid());
if(fork())
{
printf("child2 id: %d parent2 id: %d\n",getpid(),getppid());
if(fork())
printf("child3 id: %d parent3 id: %d\n",getpid(),getppid());
else
printf("child3 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child2 id: %d parent id:%d\n",getpid(),getppid());
}
else
printf("child1 id: %d parent id: %d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex7.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child1 id: 3551 parent1 id: 2817
child1 id: 3552 parent id: 3551
child2 id: 3551 parent2 id: 2817
child2 id: 3553 parent id:3551
child3 id: 3551 parent3 id: 2817
child3 id: 3554 parent id:3551
program to execute multiple processes through command line arguments
#include"myHeader.h"
main(int argc,char **argv)
{
int i;
for(i=1;i<argc;i++)
{
system(argv[i]);
}
printf("task completed...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex9.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out cal date madan
     April 2014       
Su Mo Tu We Th Fr Sa  
       1  2  3  4  5  
 6  7  8  9 10 11 12  
13 14 15 16 17 18 19  
20 21 22 23 24 25 26  
27 28 29 30           
                      
Fri Apr  4 07:12:10 IST 2014
sh: 1: madan: not found
task completed...
Program for vfork
#include"myHeader.h"
int data=0;
main(int argc,char **argv)
{
int ret;
ret=vfork();
if(ret==0)
{
printf("in child process:data=%d\n",data);
data++;
printf("in child process:data(inc)=%d\n",data);
}
else
{
printf("in parent process:data=%d\n",data);
data++;
printf("in parent process:data(inc)=%d\n",data);
}
exit(0);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ vi ex10.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex10.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in child process:data=0
in child process:data(inc)=1
in parent process:data=1
in parent process:data(inc)=2
program to print long listing of files using execlp
#include"myHeader.h"
main(int argc,char **argv)
{
execlp("ls","ls","-li","-t",NULL);
}
output:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
total 2884
4325487 -rwxr-xr-x 1 madan madan    8560 Apr  4 07:17 a.out
4325511 -rw-r--r-- 1 madan madan      87 Apr  4 07:17 ex11.c
4325510 -rw-r--r-- 1 madan madan     319 Apr  4 07:17 ex10.c
4325509 -rw-r--r-- 1 madan madan     131 Apr  4 07:14 ex9.c
4325508 -rw-r--r-- 1 madan madan     234 Apr  4 07:10 ex8.c
4325507 -rw-r--r-- 1 madan madan     453 Apr  4 07:04 ex7.c
4325505 -rw-r--r-- 1 madan madan     306 Apr  4 07:02 ex6.c
4325493 -rw-r--r-- 1 madan madan     281 Apr  4 06:56 ex5.c
4325506 -rw-r--r-- 1 madan madan     613 Apr  4 06:55 myHeader.h
4325496 -rw-r--r-- 1 madan madan     230 Apr  4 06:51 ex4.c
4325495 -rw-r--r-- 1 madan madan     104 Apr  4 06:49 ex3.c
4325494 -rw-r--r-- 1 madan madan     322 Apr  4 06:49 ex2.c
4325488 -rw-r--r-- 1 madan madan 2886512 Apr  4 06:44 myHeader.h.gch
4325490 -rw-r--r-- 1 madan madan      48 Apr  4 06:43 madan
4325491 -rw-r--r-- 1 madan madan     277 Apr  4 06:43 ex1.c
fork system call example
#include"myHeader.h"
main()
{
if(fork())
{
printf("in parent process...\n");
printf("process id:%d\n",getpid());
}
else
{
printf("in child process...\n");
printf("child process id:%d\n",getpid());
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex12.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent process...
process id:3891
in child process...
child process id:3892
example program for parrellel processing
#include"myHeader.h"
int pid[3];
main()
{
int status;
if(pid[2]==fork())
{
printf("in parent2..\n");
wait(-1,&status,NULL);
if(pid[1]==fork())
{
printf("in parent1..\n");
if(pid[0]==fork())
printf("in parent0..\n");
else
{
sleep(1);
printf("in child0 process...\n");
}
}
else
{
sleep(10);
printf("in child1 process...\n");
}
}
else
{
sleep(5);
printf("in child2 process...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex13.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent2..
in parent1..
in parent0..
in child0 process...
in child2 process...
madan@madan-Lenovo-G570:~/madan/osconcepts$ in child1 process...
parent process waits for child process
#include"myHeader.h"
main()
{
if(fork())
{
printf("in parent...\n");
sleep(2);
printf("parent is terminating...\n");
}
else
{
printf("in child...\n");
sleep(7);
while(1);
printf("child is terminating...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex14.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent...
in child...
parent is terminating...
Program for process synchronisation using waitpid
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/resource.h>
int p[2];
pipe(p);
int t;
char str1[100];
void child1()
{
printf("child1 id:%d parent id:%d\n",getpid(),getppid());
char str1[100]=("child1 is terminating first\n");
printf("in child process1:\n");
srand(getpid());
t1=rand()%10+1;
printf("in child process1, time delay:%d\n",t);
sleep(t1);
write(p[1],t1,100);
printf("child1 process is exiting..\n");
}
void child2()
{
printf("child2 id:%d parent id:%d\n",getpid(),getppid());
char str1[100]=("child2 is terminating first\n");
printf("in child process2:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process2, time delay:%d\n",t);
sleep(t);
write(p[1],str1,100);
printf("child2 process is exiting..\n");
}
void child3()
{
printf("child3 id:%d parent id:%d\n",getpid(),getppid());
char str1[100]=("child3 is terminating first\n");
printf("in child process3:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process3, time delay:%d\n",t);
sleep(t);
write(p[1],str1,100);
printf("child3 process is exiting..\n");
}

void parent()
{
int status;
printf(" in parent process...\n");
wait(&status);
status=status>>8;
read(p[0],str1,20);
printf("%s ",str1);
printf("parent process is exiting...\n");
}

main()
{
if(fork())
{
if(fork())
{
if(fork())
parent();
else
child1();
}
else
child2();
}
else
child3();
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex15.c
ex15.c:7:1: warning: data definition has no type or storage class [enabled by default]
 pipe(p);
 ^
ex15.c:7:1: warning: parameter names (without types) in function declaration [enabled by default]
ex15.c: In function �child1�:
ex15.c:19:1: warning: passing argument 2 of �write� makes pointer from integer without a cast [enabled by default]
 write(p[1],t1,100);
 ^
In file included from myHeader.h:10:0:
/usr/include/unistd.h:366:16: note: expected �const void *� but argument is of type �int�
 extern ssize_t write (int __fd, const void *__buf, size_t __n) __wur;
                ^
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
 in parent process...
child2 id:4256 parent id:4254
in child process2:
in child process2, time delay:2
child3 id:4255 parent id:4254
in child process3:
in child process3, time delay:1
child1 id:4257 parent id:4254
in child process1:
in child process1, time delay:0
child3 is terminating first
child3 process is exiting..
child2 is terminating first
child2 process is exiting..
child1 process is exiting..


 parent process is exiting...
Program to calculate arithmetic expressions in child processes
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/resource.h>
int t;
int a=20,b=10,sum,sub,mul;
int child1()
{
printf("child1 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process1:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process1, time delay:%d\n",t);
sleep(t);
sum=a+b;
return sum;
printf("child1 process is exiting..\n");
}
int child2()
{
printf("child2 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process2:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process2, time delay:%d\n",t);
sleep(t);
sub=a-b;
return sub;
printf("child2 process is exiting..\n");
}
int child3()
{
printf("child3 id:%d parent id:%d\n",getpid(),getppid());
printf("in child process3:\n");
srand(getpid());
t=rand()%10+1;
printf("in child process3, time delay:%d\n",t);
sleep(t);
mul=a*b;
return mul;
printf("child3 process is exiting..\n");
}

int parent()
{
int status;
printf(" in parent process...\n");
wait(&status);
status=status>>8;
printf("first termionating is:%d\n",status);
printf("parent process is exiting...\n");
}

main()
{
if(fork())
{
if(fork())
{
if(fork())
parent();
else
printf("multiplication=%d\n",child3());
}
else
printf("substraction=%d\n",child2());
}
else
printf("addition=%d\n",child1());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex16.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
 in parent process...
child3 id:4407 parent id:4404
in child process3:
in child process3, time delay:9
child2 id:4406 parent id:4404
in child process2:
child1 id:4405 parent id:4404
in child process1:
in child process1, time delay:1
in child process2, time delay:4
addition of two numbers=30
first termionating is:27
parent process is exiting...
madan@madan-Lenovo-G570:~/madan/osconcepts$ substraction=10
multiplication=200
example for pause system call
#include"myHeader.h"
main()
{
int a=10;
if(fork())
{
printf("%d ",a);
pause();
}
else
{
printf("%d ",a++);
printf("%d ",a);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex17.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
10 11
program to find size of pipe
#include"myHeader.h"
main(int argc,char **argv)
{
int cnt=0;
int p[2];
pipe(p);
fcntl(p[1],F_SETFL,O_NONBLOCK);
while(1)
{
if(write(p[1],"a",1)==1)
cnt++;
else
break;
}
printf("count=%d\n",cnt);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
count=65536
program to print character by character from file named madan
#include"myHeader.h"
main(int argc,char **argv)
{
char ch;
int fd;
fd=open(argv[1],O_RDONLY);
while(read(fd,&ch,1)==1)
{
printf("ch= %c ",ch);
write(1,&ch,1);
}
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
welcome madan reddych= w ch= e ch= l ch= c ch= o ch= m ch= e ch=   ch= m ch= a ch= d ch= a ch= n ch=   ch= r ch= e ch= d ch= d ch= y ch= 

hyderabad ch= h ch= y ch= d ch= e ch= r ch= a ch= b ch= a ch= d ch= 

always truth wins ch= a ch= l ch= w ch= a ch= y ch= s ch=   ch= t ch= r ch= u ch= t ch= h ch=   ch= w ch= i ch= n ch= s ch=
program to compare two files using stat
#include"myHeader.h"
main(int argc,char **argv)
{
struct stat buf;
struct stat buf1;
if(stat(argv[1],&buf)==-1)
{
perror("stat");
exit(0);
}
printf("%o..\n",&buf.st_mode);
printf("%u..\n",&buf.st_size);
if(stat(argv[2],&buf1)==-1)
{
perror("stat");
exit(0);
}
printf("%o..\n",&buf1.st_mode);
printf("%u..\n",&buf1.st_size);

if((&buf.st_mode)==(&buf1.st_mode))
printf("two files are same...\n");
else
printf("two files are not same...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ vi ex20.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex20.c
ex20.c: In function �main�:
ex20.c:11:1: warning: format �%o� expects argument of type �unsigned int�, but argument 2 has type �__mode_t *� [-Wformat=]
 printf("%o..\n",&buf.st_mode);
 ^
ex20.c:12:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �__off_t *� [-Wformat=]
 printf("%u..\n",&buf.st_size);
 ^
ex20.c:18:1: warning: format �%o� expects argument of type �unsigned int�, but argument 2 has type �__mode_t *� [-Wformat=]
 printf("%o..\n",&buf1.st_mode);
 ^
ex20.c:19:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �__off_t *� [-Wformat=]
 printf("%u..\n",&buf1.st_size);
 ^
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan madan
25763044350..
2949400832..
25763044570..
2949400976..
two files are not same...

program for time stamp updation using utime
#include"myHeader.h"
main(int argc,char **argv)
{
struct utimbuf v;

v.actime=v.modtime=time(0);
if((utime(argv[1],&v)==-1))
{
perror("utime");
exit(0);
}
printf("access time:%d modification time:%d\n",v.actime,v.modtime);
printf("time stamp updated..\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex21.c
ex21.c: In function �main�:
ex21.c:12:1: warning: format �%d� expects argument of type �int�, but argument 2 has type �__time_t� [-Wformat=]
 printf("access time:%d modification time:%d\n",v.actime,v.modtime);
 ^
ex21.c:12:1: warning: format �%d� expects argument of type �int�, but argument 3 has type �__time_t� [-Wformat=]
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
access time:1396578347 modification time:1396578347
time stamp updated..
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
utime: Bad address

program to update modification details of two files
#include"myHeader.h"
main(int argc,char **argv)
{
struct utimbuf v1;
if(utime(argv[1],&v1)==-1)
{
perror("utime");
exit(0);
}
v1.actime=v1.modtime=time(0);
struct utimbuf v2;
if(utime(argv[2],&v2)==-1)
{
perror("utime");
exit(0);
}
v2.actime=v2.modtime=time(0);
if(v1.modtime==v2.modtime)
printf("two time stamps have same mod time...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cat madan
welcome madan reddy
hyderabad
always truth wins
madan@madan-Lenovo-G570:~/madan/osconcepts$ cat mohan
welcome htderabad
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex22.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan mohan
two time stamps have same mod time...
program for parent process waits for the status of child process
#include"myHeader.h"
int data=10;
main()
{
int status;
if(fork()==0)
{
printf("data in child process:%d\n",data);
sleep(5);
printf("child exiting...\n");
}
else
{
printf("data in parent process:%d\n",data);
wait(&status);
printf("parent exiting....\n");
//sleep(2);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex23.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
data in parent process:10
data in child process:10
child exiting...
parent exiting....
Example program for sleep
#include"myHeader.h"
int pid[3];
main()
{
int status;
if(pid[2]==fork())
{
printf("in parent2..\n");
wait(-1,&status,NULL);
if(pid[1]==fork())
{
printf("in parent1..\n");
if(pid[0]==fork())
printf("in parent0..\n");
else
{
sleep(1);
printf("in child0 process(1sec)...\n");
}
}
else
{
sleep(10);
printf("in child1 process(10sec)...\n");
}
}
else
{
sleep(5);
printf("in child2 process(5sec)...\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex24.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in parent2..
in parent1..
in parent0..
in child0 process(1sec)...
in child2 process(5sec)...
madan@madan-Lenovo-G570:~/madan/osconcepts$ in child1 process(10sec)...
Example program for wait(&status)
#include"myHeader.h"
main()
{
int i,status;
if(fork()>0)
{
printf("parent id:%d\n",getppid());
for(i=6;i<10;i++)
{
printf("i=%d\n",i);
sleep(1);
}
wait(&status);
}
else if(fork()==0)
{
printf("child id:%d\n",getpid());
for(i=10;i<25;i++)
{
printf("i=%d\n",i);
sleep(1);
}
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex25.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent id:2817
i=6
child id:4930
i=10
i=7
i=11
i=8
i=12
i=9
i=13
i=14
madan@madan-Lenovo-G570:~/madan/osconcepts$ i=15
i=16
i=17
i=18
i=19
i=20
i=21
i=22
i=23
i=24
Example program for fork system call
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
if(fork())
printf("parent=%d bash=%d\n",getpid(),getppid());
else
if(fork())
printf("child4==%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child3=%d parent=%d\n",getpid(),getppid());
else
if(fork())
printf("child2=%d parent=%d\n",getpid(),getppid());
else
printf("child1=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex26.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=4954 bash=2817
madan@madan-Lenovo-G570:~/madan/osconcepts$ child4==4955 parent=1144
child3=4956 parent=4955
child2=4957 parent=1144
child1=4958 parent=4957
Program to find size of pipe
#include"myHeader.h"
int i=0;
main()
{
int count=0;
int fp[2];
pipe(fp);
while(1)
{
printf("count=%d\n",count);
count++;
write(fp[1],&count,1);
}
}
OutPut:

count=65535
count=65536
Example program on preprocessor directives
#include<stdio.h>
#include<stdlib.h>
#define a 2+5
//#define a 5-2
main()
{
int i;
i=a*a*a;
printf("result:%d\n",i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex30.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
result:27
program to view differences between process id, parent process id, group id
#include"myHeader.h"
main()
{
printf("child:%d parent %d groupid:%d uid:%d\n",getpid(),getppid(),getgid(),getuid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex31.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
child:5444 parent 2817 groupid:1000 uid:1000
Program to view relationship among process id's
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
if(fork())
{
//system("date");
        printf("parent=%d bash=%d\n",getpid(),getppid());

        if(fork())
        {
//      system("cal");
        printf("parent=%d bash=%d\n",getpid(),getppid());
        if(fork())
        {
        printf("parent=%d bash=%d\n",getpid(),getppid());
        if(fork())
        printf("parent=%d bash=%d\n",getpid(),getppid());
                else
                printf("in process3 child3=%d parent= %d...\n",getpid(),getppid());
        }
        else
        printf("in process2 child2= %d parent=%d...\n",getpid(),getppid());
        }
else
printf("in process1 child1= %d parent=%d...\n",getpid(),getppid());
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex32.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
parent=5464 bash=2817
parent=5464 bash=2817
in process1 child1= 5466 parent=5464...
parent=5464 bash=2817
parent=5464 bash=2817
in process2 child2= 5467 parent=5464...
in process3 child3=5468 parent= 1144...
Examle program to refer process id's
#include"myHeader.h"
main()
{
int i;
printf("process:%d\n",getpid());
for(i=1;i<=3;i++)
{
fork();
printf("process id:%d\n",getpid());
}
printf("exiting ......\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex33.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
process:5492
process id:5492
process id:5492
process id:5494
process id:5492
exiting ......
process id:5494
exiting ......
process id:5495
exiting ......
process id:5496
exiting ......
process id:5493
process id:5493
process id:5493
exiting ......
madan@madan-Lenovo-G570:~/madan/osconcepts$ process id:5497
process id:5498
process id:5497
exiting ......
exiting ......
process id:5499
exiting ......
program to print file descriptor value
#include"myHeader.h"
main(int argc,char **argv)
{
int ret,i;
char readbuf[30];
ret=open(argv[1],O_RDONLY);
if(argc!=2)
printf("enter correct arguments\n");
if(ret==-1)
{
perror("open");
return;
}
printf("file description:%d\n",ret);
while((read(ret,readbuf,30)))
printf("%s",readbuf);
//readbuf[20]='\0';
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex34.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
file description:5
welcome madan reddy
hyderabad
always truth wins
y
hyderabad
Program to read a file
#include"myHeader.h"
main(int argc,char **argv)
{
char ch;
int fd;
fd=open(argv[1],O_RDONLY);
if(fd==-1)
{
perror("open");
return;
}
while(read(fd,&ch,1)==1)
{
write(1,&ch,1);
//printf("%c",ch);
}
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out madan
welcome madan reddy
hyderabad
always truth wins
program to view semaphore operation
#include"myHeader.h"
main(int argc,char **argv)
{
int id;
struct sembuf v;
id=semget(1,3,IPC_CREAT|0600);
printf("id=%d in process %d\n",id,getpid());
printf("about to pend on semaphore\n");
v.sem_num=0;
v.sem_op=0;
v.sem_flg=0;
semop(id,&v,1);
printf("after semop on critical region\n");
printf("process exiting...\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex36.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
id=0 in process 5741
about to pend on semaphore
after semop on critical region
process exiting...
program to lock a file
#include"myHeader.h"
#include<sys/stat.h>
#include<sys/ipc.h>
main()
{
struct flock lockbuf;
int fd;
fd=open("datafile",O_RDWR);
getchar();
printf("enter to lock file...\n");
lockbuf.l_type=F_WRLCK;
lockbuf.l_whence=0;
lockbuf.l_start=0;
lockbuf.l_len=0;
if(fcntl(fd,F_SETLKW,&lockbuf)<0)
perror("fcntl");
printf("LOCKED");
close(fd);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out

enter to lock file...
fcntl: Bad file descriptor
LOCKED
Alarm program by using sigaction
#include"myHeader.h"
void isr(int n)
{
printf("ALARM.....\n");
alarm(2);
}
main()
{
struct sigaction v;
printf("%d\n",getpid());
alarm(5);
v.sa_handler=isr;
v.sa_flags=SA_RESETHAND;
v.sa_flags=0;
sigemptyset(&v.sa_mask);
sigaction(SIGALRM,&v,NULL);
printf("ALARM is set...\n");
printf("in infinite loop....\n");
while(1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
5876
ALARM is set...
in infinite loop....

ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....
ALARM.....

ALARM.....
ALARM.....
^C
Program for sigalrm
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
void isr(int n)
{
static int i=0;
printf("in ISR......\n");
if(++i==3)
exit(0);
alarm(2);
}
main()
{
printf("proces:%d\n",getpid());
//signal(SIGINT,SIG_DFL);
alarm(3);
signal(SIGALRM,isr);
printf("alarm is set....\n");
//printf("in infinite loop...\n");

while(1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex40.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
proces:5897
alarm is set....
in ISR......
in ISR......
in ISR......
srand to provide random time delays
#include<stdio.h>
#include<stdlib.h>


void child1(void)
{
int t;
printf("in child1\n");
srand(getpid());
t=rand()%10+1;
printf("child1 delay for %d seconds\n",t);
sleep(t);
printf("child1 exiting....\n");
exit(0);
}
void child2(void)
{
int t;
printf("in child2\n");
srand(getpid());
t=rand()%10+1;
printf("child2 delay for %d seconds\n",t);
sleep(t);
printf("child2 exiting....\n");
exit(0);
}
void parent(void)
{
int status;
printf("parent waiting...\n");
while(1){
wait(&status);
status>>=8;
if(status==2)
break;
}
printf("child exiting...\n");
printf("parent exiting....\n");
exit(0);
}
main()
{
if(fork())
{
 if(fork())
 parent();
 else
 child2();
}
else
child1();
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
in child1
parent waiting...
child1 delay for 2 seconds
in child2
child2 delay for 3 seconds
child1 exiting....
child2 exiting....
program to calculate arithmatic operation in child process and print result in parent process
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int child(int a,int b)
{
int sum;
sum=a+b;
sleep(5);
return(sum);
//parent(sum);
}
int parent(int sum)
{
int status;
wait(&status);
status>>=8;
//printf("sum=%d\n",sum);
//printf("sum=%d\n",sum);
}
main(int argc,char **argv)
{
int a,b,sum=0;
if(argc!=3)
printf("enter correct no of arguments\n");
a=atoi(argv[1]);
b=atoi(argv[2]);
if(fork())
printf("sum in parent process is=%d\n",parent(sum));
//printf("sum in parent process=%d\n",parent(sum));
else
printf("sum in child process=%d\n",child(a,b));
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex43.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out 10 20
sum in child process=30
sum in parent process is=24
Program for system call
#include<stdio.h>
#include<sys/resource.h>a
#include"myHeader.h"
main()
{
printf("system=%d parent=%d\n",getpid(),getppid());
printf("p2 id=%d \tp2 parent=%d\n",getpid(),getppid());
system("date");
system("cal");
system("./system");
printf("p2=%d parent=%d\n",getpid(),getppid());
}
OutPut:
madan@madan-Lenovo-G570:~/madan/osconcepts$ cc ex44.c
madan@madan-Lenovo-G570:~/madan/osconcepts$ ./a.out
system=6169 parent=2817
p2 id=6169  p2 parent=2817
Fri Apr  4 09:10:48 IST 2014
     April 2014       
Su Mo Tu We Th Fr Sa  
       1  2  3  4  5  
 6  7  8  9 10 11 12  
13 14 15 16 17 18 19  
20 21 22 23 24 25 26  
27 28 29 30           
                      
sh: 1: ./system: not found
p2=6169 parent=2817
Read More

Pointers

Pointers1
#include<stdio.h>
main()
{
int i=0x12345678;
int *p=&i;
printf("i=%x\n",i);
printf("i=%x\n",*(int *)p);
printf("i=%x\n",*(double *)p);
printf("i=%x\n",*(short int *)p);
char ch='B';
char *cp=&ch;
double d=12.5;
double *dp=&d;
printf("ch=%d at %u d=%f at %u\n",*cp,&ch,*dp,&d);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer1.c
pointer1.c: In function �main�:
pointer1.c:8:1: warning: format �%x� expects argument of type �unsigned int�, but argument 2 has type �double� [-Wformat=]
 printf("i=%x\n",*(double *)p);
 ^
pointer1.c:14:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �char *� [-Wformat=]
 printf("ch=%d at %u d=%f at %u\n",*cp,&ch,*dp,&d);
 ^
pointer1.c:14:1: warning: format �%u� expects argument of type �unsigned int�, but argument 5 has type �double *� [-Wformat=]
madan@madan-Lenovo-G570:~/madan$ ./a.out
i=12345678
i=12345678
i=7ffffff5
i=5678
ch=66 at 2484735611 d=12.500000 at 2484735616
pointers-2
#include<stdio.h>
main()
{
int choice,c;
printf("enter ur choice\n");
scanf("%d",&choice);
int a=10,b=20;
int *ipa=&a,*ipb=&b,*ipc=&c;
switch(choice)
{
case 1:
//c=a+b;
printf("c=%d at %u",*ipc=*ipa+*ipb,ipc);
break;
case 2:
printf("c=%d at %u\n",*ipc=*ipa-*ipb,ipc);
break;
default:printf("enter currect choice\n");
}
printf("\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer2.c
pointer2.c: In function �main�:
pointer2.c:13:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=]
 printf("c=%d at %u",*ipc=*ipa+*ipb,ipc);
 ^
pointer2.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=]
 printf("c=%d at %u\n",*ipc=*ipa-*ipb,ipc);
 ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter ur choice
1
c=30 at 2295102972
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter ur choice
2
c=-10 at 88970556
Pointers-3
#include<stdio.h>
main()
{
float f=12.5;
float *p=&f;
char c,*cp=&f;
int i,*ip=&f;
float g;
g=*p;
i=*ip;
c=*cp;

printf("g=%f\n",g);
printf("i=%d\n",i);
printf("c=%c\n",c);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer3.c
pointer3.c: In function �main�:
pointer3.c:6:12: warning: initialization from incompatible pointer type [enabled by default]
 char c,*cp=&f;
            ^
pointer3.c:7:11: warning: initialization from incompatible pointer type [enabled by default]
 int i,*ip=&f;
           ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
g=12.500000
i=1095237632
c=
Pointers to check binary Pattern
#include<stdio.h>
main()
{
float f=10.5;
char c,*cp=&f;
int i,n,j;
printf("enter value for n\n");
scanf("%d",&n);
for(i=n;i>=0;i--)
{
c=*(cp+i);
for(j=7;j>=0;j--)
printf("%d ",(c>>j)&1?1:0);
printf("\n");
}
printf("\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer4.c
pointer4.c: In function �main�:
pointer4.c:5:12: warning: initialization from incompatible pointer type [enabled by default]
 char c,*cp=&f;
            ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
7
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 1 1 1 
0 1 0 0 0 0 0 1 
0 0 1 0 1 0 0 0 
0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0
Increment Operation Through pointers
#include<stdio.h>
main()
{
int a=10,b=15,c;
int *ip1=&a,*ip2=&b;
//a=*ip1;
//b=*ip2;
a=*ip1;
printf("a=%d\n",a);
a=(*ip1)++;
printf("a=%d\n",a);
//c=*ip1+*ip2;
//printf("%d %d %d\n",*ip1,*ip2,c);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer5.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
a=10
a=10

command line arguments printing
#include<stdio.h>
main(int n,char **argv)
{
int i;
printf("argc=%d\n",n);
for(i=1;i<=n;i++)
printf("argv[%d]=%s\n",i,argv[i]);
}
Output:
madan@madan-Lenovo-G570:~/madan$ cc pointer6.c
madan@madan-Lenovo-G570:~/madan$ ./a.out 12 34 54 65
argc=5
argv[1]=12
argv[2]=34
argv[3]=54
argv[4]=65
argv[5]=(null)

PointersExample
#include<stdio.h>
main()
{
int i,j,a[2][3];
printf("enter 2D array\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("a[%d][%d]=%d\n",i,j,a[i][j]);
printf("%u %u %u %d %d\n",a,*a,*a+1,a[0],a[0][2]);
printf("%u %u %u\n",*(a+1),*((*a+1)+1),**(a+1));
}
Output:
madan@madan-Lenovo-G570:~/madan$ cc pointer7.c
pointer7.c: In function �main�:
pointer7.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �int (*)[3]� [-Wformat=]
 printf("%u %u %u %d %d\n",a,*a,*a+1,a[0],a[0][2]);  
 ^
pointer7.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 3 has type �int *� [-Wformat=]
pointer7.c:16:1: warning: format �%u� expects argument of type �unsigned int�, but argument 4 has type �int *� [-Wformat=]
pointer7.c:16:1: warning: format �%d� expects argument of type �int�, but argument 5 has type �int *� [-Wformat=]
pointer7.c:17:1: warning: format �%u� expects argument of type �unsigned int�, but argument 2 has type �int *� [-Wformat=]
 printf("%u %u %u\n",*(a+1),*((*a+1)+1),**(a+1));  
 ^
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter 2D array
1
2
3
4
5
6     
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
642338032 642338032 642338036 642338032 3
642338044 3 4
Array Printing...
#include<stdio.h>
main()
{
char str[5][10],i,j;
printf("enter array elements\n");
for(i=0;i<5;i++)
scanf("%s",str[i]);
printf("resultant array is...\n");
for(i=0;i<5;i++)
printf("%s\n",str[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer8.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter array elements
well
come
madan     
hyderabad
india
resultant array is...
well
come
madan
hyderabad
india
2D_Array Passing by Using Pointers
#include<stdio.h>
void print(int (*p)[3],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
printf("%d\n",p[i][j]);
}
main()
{
int a[2][3]={{11,22,33},{44,55,66}};
print(a,2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pointer9.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
11
22
33
44
55
66
Read More

Simple C Programs

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=31
program 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 1
program 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 20
Example 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.000000
Program 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=-32768
Program for Logical operations checking
#include
#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);
}
OutPut:
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
0
Program 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
0
Program 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 D
Program 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 50
Program 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 7
Program 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 32
program 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 2893998576
program 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 81
program 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 15
program 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 2027025
program 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=30
Program 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 503631816
program 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
11110000000000000000000000000000
Program 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 1
Program 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=32
Program 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 100
Cube 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=125
Sum 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
Read More

Pattern Printing

Program to print particular pattern
#include<stdio.h>
main()
{
int i,j,n;
char ch='a';
printf("eneter value for n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
//printf("\n%d\n",i);
 for(j=1;j<=i;j++)
 {
 printf("%5c",ch++);
 }
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex27.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
eneter value for n
5
    a
    b    c
    d    e    f
    g    h    i    j
    k    l    m    n    o
Program to print RHOMBOUS Pattern
#include<stdio.h>
main()
{
int i,j,n,k;
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
        for(j=1;j<=n;j++)
        {
        k=i;
        if(k<0)
        k=-1*k;
        if(j<=k)
        printf(" ");
        else
        printf("* ");
        }
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex28.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
     
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    *
Program pattern for REVERSE RHOMBOUS
#include<stdio.h>
main()
{
int i,j,k,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
for(j=n;j>=0;j--)
{
        k=i;
        if(k<0)
        k=-k;
if(j>=k)
printf(" ");
else
printf("* ");
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex29.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
      
     * 
    * * 
   * * * 
  * * * * 
 * * * * *
Program to print following pattern
#include<stdio.h>
main()
{
int i,j,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=-n;j>=n+1;j++)
printf(" "); 
for(j=n;j>=i;j--)
printf("* "); 
printf("\n");
}
for(i=1;i<=n+1;i++) 
{
for(j=1;j<=n;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
}
} 
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex30.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
* * * * * 
* * * * 
* * * 
* * 
* 
     * 
     * * 
     * * * 
     * * * * 
     * * * * * 
     * * * * * *
program to print triangle pattern
#include<stdio.h>
main()
{
int i,j,n;
printf("enter value for n\n");
scanf("%d",&n);
/*i=1;
while(i<=5)
{
 j=1;
 while(j<=10)
 {
 printf("%d\t",i*j);
 j++;
 }
i++;
printf("\n");
}*/

/*for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}*/

/*
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
printf(" ");
for(j=1;j<=i;j++)
{
printf(" * ");
}
}
printf("\n");
}*/

for(i=n;i>0;i--)
{
for(j=1;j<=n;j++)
{
if(j<i)
printf(" ");
else
printf("* ");
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex46.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
    * 
   * * 
  * * * 
 * * * * 
* * * * *
Program to print pattern
#include<stdio.h>
main()
{
int i,j,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
printf(" ");
for(j=1;j<=i;j++)
printf(" * ");
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
     * 
     *  * 
     *  *  * 
     *  *  *  * 
     *  *  *  *  *
Program to print pattern
#include<stdio.h>
main()
{
int i,j,k,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
k=i;
if(k<0)
k=-k;
for(j=0;j<n;j++)
{
if(j<=k)
printf(" ");
else
printf("* ");
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid2.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
     
     
    * 
   * * 
  * * * 
 * * * * 
  * * * 
   * * 
    *
Program to print pattern
#include<stdio.h>
main()
{
int i,j,k,n;
char ch='A';
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
k=i;
if(k<0)
k=-k;
for(j=0;j<=n;j++)
{
if(j<=k)
printf(" ");
else
{
printf("%c ",ch);
ch++;
}
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid3.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
      
     A 
    B C 
   D E F 
  G H I J 
 K L M N O 
  P Q R S 
   T U V 
    W X 
     Y
Program to print pattern
#include<stdio.h>
main()
{
int i,j,k,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=2*n-i;j++)
printf(" ");
for(j=0;j<=2*i;j++)
printf("*");
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid4.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
           *
          ***
         *****
        *******
       *********

Read More

Basic C Programs

Program to find weather given number has square root or not
#include<stdio.h>
main()
{
int n,temp,f;
printf("enter any number\n");
scanf("%d",&n);
temp=n;
if((temp%2)==0)
{
printf("square root is possible\n");
f=check(n);
}
else
printf("square root is not possible\n");
}
check(int x)
{
int count=0;
while((x/2)==0)
count++;
if((count%2)==0)
printf("power of 2 possible\n");
else
printf("power of 2 not possible\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc recurse.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number
34
square root is possible
power of 2 possible
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number
36
square root is possible
power of 2 possible
Program to evaluate an expression
#include<stdio.h>
#include<stdlib.h>
operations()
{
int a;
printf("Program for various operations\n");
printf("enter ur choice 1:tabs\n 2:manipulate\n 3:forloop\n");
scanf("%d",&a);
if(a==1)
{
printf("performing tabs operation\n");
tabs();
}
else if(a==2)
{
printf("performing manipulations..\n");
manipulate();
}
else if(a==3)
{
printf("performing forloop operation...\n");
forloop();
}
}
tabs()
{
printf("for back Space\b");
printf("for tab space\t");
printf("for coming to next line\n");
}
manipulate()
{
int a,b,result,n;
printf("read the values for a and b\n");
scanf("%d%d",&a,&b);
printf("enter ur choice N\n");
scanf("%d",&n);

switch(n)
{
case 1:
printf("addition operation\n");
result=a+b;
break;

case 2:
printf("substraction operation\n");
if(a>=b)
result=a-b;
else
result=b-a;
break;

case 3:
printf("multiplication operation\n");
result=a*b;
break;

case 4:
printf("division operation\n");
result=(a/b);
break;

default:printf("enter correct choice\n");
}
printf("result is %d\n",result);
}
forloop()
{
 int i,j,n;
char ch='a';
 printf("enter value for N\n");
   scanf("%d",&n);
  for(i=0;i<n;i++)
  {
   for(j=0;j<=i;j++)
   printf("%c ",ch++);
   printf("\n");
  }
}

main()
{
int choice;
operations();
}
OutPut:
Program for various operations
enter ur choice 1:tabs
 2:manipulate
 3:forloop
3
performing forloop operation...
enter value for N
3
a 
b c 
d e f 
madan@madan-Lenovo-G570:~/madan$ ./a.out
Program for various operations
enter ur choice 1:tabs
 2:manipulate
 3:forloop
2
performing manipulations..
read the values for a and b
12
35
enter ur choice N
4
division operation
result is 0
program to check various conditions
#include<stdio.h>
#include<stdlib.h>
main()
{
int choice;
printf("program to check various conditions:1:prime\n 2:evenodd\n 3:swapping\n 4:sign_check\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("perform prime number series\n");
prime();
break;
case 2:
printf("to check even or odd\n");
evenodd();
break;
case 3:printf("swapping operation...\n");
swapping();
break;
case 4: printf("to check sign..\n");
sign();
break;
default:printf("enter correct choice\n");
}
printf("your task successfully completed\n");
}
prime()
{
int n,i;
printf("enter value for n:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(i%2!=0)
{
printf("%d\n",i);
}
}
}
evenodd()
{
int n;
printf("enter value for n\n");
scanf("%d",&n);
if(n%2==0)
{
printf("even number\n");
}
else
{
printf("odd number\n");
}
}
swapping()
{
int a,b,c;
printf("enter values for a and b\n");
scanf("%d%d",&a,&b);
c=a;
a=b;
b=c;
printf("after swapping a and b values are %d\t%d\n",a,b);
}
sign()
{
int n;
printf("enter value for n");
scanf("%d",&n);
if(n>0)
printf("positive number\n");
else
printf("negative number\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to check various conditions:1:prime
 2:evenodd
 3:swapping
 4:sign_check
2
to check even or odd
enter value for n
45
odd number
your task successfully completed
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to check various conditions:1:prime
 2:evenodd
 3:swapping
 4:sign_check
3
swapping operation...
enter values for a and b
54
675
after swapping a and b values are 675 54
your task successfully completed
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to check various conditions:1:prime
 2:evenodd
 3:swapping
 4:sign_check
4
to check sign..
enter value for n-567
negative number
your task successfully completed
Program to examine int,float data types
#include<stdio.h>
#include<stdlib.h>
main()
{
int a=5,b=2,c;
float d=1.5,e=2.5,f;
c=(a+e);
f=(b+d);
printf("c=%d\n",c);
printf("f=%f\n",f);
}
OutPut:
c=7
f=3.500000
Program to understand data types
#include<stdio.h>
#include<stdlib.h>
datatypes()
{
int choice;
printf("to understand data types\n");
printf("1:integer\n 2:floating\n 3:chartype\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("integer operation\n");
       integer();
       break;
case 2:printf("floating point operation\n");
       floating();
       break;
case 3:printf("character type operation\n");
chartype();
break;
default:printf("enter correct choice...\n");
printf("we are in main function\n");
}
}
integer()
{
 int a,b,c,area,volume;
printf("enter values for a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
area=2*(a*b+b*c+c*a);
volume=a*b*c;
printf("area of rectangle=%d\n",area);
printf("volume of rectangle=%d\n",volume);
}
floating()
{
float a,b,c;
printf("enter values for a and b\n");
scanf("%f%f",&a,&b);
c=a+b;
printf("result(sum) of c=%f\n",c);
}
chartype()
{
char ch;
ch=getchar();
putchar(ch='a');
printf("the entered character is %c",ch);
}
main()
{
datatypes();
}
OutPut:
to understand data types
1:integer
 2:floating
 3:chartype
1
integer operation
enter values for a,b and c
1 
2
3
area of rectangle=22
volume of rectangle=6
madan@madan-Lenovo-G570:~/madan$ ./a.out
to understand data types
1:integer
 2:floating
 3:chartype
2
floating point operation
enter values for a and b
12.33
23.44
result(sum) of c=35.770000
madan@madan-Lenovo-G570:~/madan$ ./a.out
to understand data types
1:integer
 2:floating
 3:chartype
3
character type operation
athe entered character is a
Program to print sizes of various data types
#include<stdio.h>
#include<stdlib.h>
main()
{
int a=5;
float c=1.5;
printf("size of integer type is %d\n",sizeof(int));
printf("size of float type is %d\n",sizeof(float));
printf("size of double type is %d\n",sizeof(double));
printf("size of char type is %d\n",sizeof(char));
printf("size of a is %d\n",sizeof(a));
printf("size of F is %d\n",sizeof(1.5));
}
OutPut:
size of integer type is 4
size of float type is 4
size of double type is 8
size of char type is 1
size of a is 4
size of F is 8
Program to examine loops concept
#include<stdio.h>
#include<stdlib.h>
main()
{
int i=0,n;
printf("enter value for n\n");
scanf("%d",&n);
for(;;)
{
do
{
printf("%d\t",i);
i++;
}while(i<n);
break;
}
}
OutPut:
enter value for n
5
0 1 2 3 4
Program to print student information
void name(void);
void age(void);
studentdata()
{
printf("Student information\n");
name();
age();
printf("we are in main function\n");
exit(0);
}
void name(void)
{
 char ch;
 printf("enter any character between A-Z\n");
 ch=getchar();
 putchar(ch);
 if((ch>='a')&&(ch<='n'))
 {
  printf("name is MADANREDDY\n");
 }
 else
 {
  printf("name is SURESH\n");
 }
}
void age(void)
{
int n;
printf("enter value for n:\n");
scanf("%d",&n);
if((n>0)&&(n<14))
{
printf("child\n");
}
else if((n>14)&&(n<30))
{
printf("young age\n");
}
else if((n>30)&&(n<60))
{
printf("middle age\n");
}
else if(n>60)
{
printf("old age");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi studentinfo.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles studentinfo.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
Student information
enter any character between A-Z
e
ename is MADANREDDY
enter value for n:
56
middle age
we are in main function
Example program to understood header
/*author:B2
  institute:vector
  program written to check preprocessor stage*/
main()
{
 int a,b; //identifiers
 printf("enter values for a and b:\n:");
 scanf("%d%d",&a,&b);
  if(a>b) //loop declaration
{
printf("A is Bigger one\n");
}
else
{
printf("B is bigger\n");
}
}
#include<stdio.h>
OutPut:
enter values for a and b:
:12
43
B is bigger
Program to examine various loops
#include<stdio.h>
#include<stdlib.h>
loops()
{
printf("program to define loops\n");
printf("**************************\n");
forloop();
whileloop();
dowhile();
exit(0);
}
forloop()
{
 int i,n,sum=0;
printf("program to print sum of first N integer values\t");
printf("enter value for N\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
sum=sum+i;
}
printf("sum ofn integers is %d\n",sum);
}
whileloop()
{
 int i=1,n,mul=1;
printf("program to multiply first N integers\t");
printf("***************************************\n");
printf("enter value for n\n");
scanf("%d",&n);
while(i<n)
{
 mul=mul*i;
i++;
}
printf("multiplication=%d\n",mul);
}
dowhile()
{
int i=0,n;
printf("program to show first N integers\n");
printf("**********************************\n");
printf("enter value for N\n");
scanf("%d",&n);
do
{
  printf("the numbers are %d\n",i);
i++;
}while(i<n);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi operations.c
madan@madan-Lenovo-G570:~/madan$ vi operations.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles operations.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400440
madan@madan-Lenovo-G570:~/madan$ ./a.out
program to define loops
**************************
program to print sum of first N integer values enter value for N
5
sum ofn integers is 10
program to multiply first N integers
enter value for n
5
multiplication=120
program to show first N integers
enter value for N
5
the numbers are 0
the numbers are 1
the numbers are 2
the numbers are 3
the numbers are 4
Program without main
#include<stdio.h>
#include<stdlib.h>
vector()
{
printf("program without main function...\n");
f1();
f2();
printf("we are in vector function\n");
exit(0);
}
f1()
{
printf("we are in f1 function\n");
}
f2()
{
printf("we are in f2 function\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi without_main.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles without_main.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400390
madan@madan-Lenovo-G570:~/madan$ ./a.out
program without main function...
we are in f1 function
we are in f2 function
we are in vector function

Program to print average of two numbers
#include<stdio.h>
main()
{
 int a,b;
 float c;
 printf("enter two values:\n");
 scanf("%d%d",&a,&b);
 c=(float)(a+b)/2;
 printf(" average of first two numbers is %f",c);
} 
OutPut:
enter two values:
1
24 
 average of first two numbers is 12.500000
Program to access a character from getchar function
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}
OutPut:
enter any character:a
entered character:a
Program to explain escape sequences
#include<stdio.h>
#include<stdlib.h>
main()
{
printf("lineSpace:\n");
printf("tabSpace:\t");
printf("backspace:\b");
}
OutPut:
lineSpace:
tabSpace: backspace
program without main with 2 functions
#include<stdio.h>
#include<stdlib.h>
vector()
{
printf("function with   main....\n");
f1();
f2();
printf("we are in main function\n");
exit(0);
}
f1()
{
printf("we are in f1 function\n");
}
f2()
{
printf("we are in f2 function\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi with2fun.c
madan@madan-Lenovo-G570:~/madan$ cc -nostartfiles with2fun.c
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400390
madan@madan-Lenovo-G570:~/madan$ ./a.out
function with   main....
we are in f1 function
we are in f2 function
we are in main function
program to reverse a number
#include<stdio.h>
#include<string.h> 
main()
{
int n,i,result=0;;
printf("enter any number...\n");
scanf("%d",&n);
printf("number before reverse..%d\n",n);
while(n>0)
{
result=result*10;
result=result+(n%10);
n=n/10;
}
printf("reversed number is=%d\n",result);
}
OutPut:
enter any number...
12
number before reverse..12
reversed number is=21
Program for armstrong number
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//////////////////////////
void arms(int n)
{
int temp,rem,sum=0;
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
printf("sum:%d\n",sum);
if(sum==temp)
printf("armstrong number...\n");
else
printf("not an armstrong number..\n");
}


main(int argc,char **argv)
{
int n,sum=0,rem,temp,i;
//if(argc!=2)
//printf("enter correct number of arguments...\n");
for(i=1;i<argc;i++)
{
n=atoi(argv[i]);
arms(n);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ vi arm.c
madan@madan-Lenovo-G570:~/madan$ cc arm.c
madan@madan-Lenovo-G570:~/madan$ ./a.out 153
sum:153
armstrong number...
madan@madan-Lenovo-G570:~/madan$ ./a.out 153
sum:153
armstrong number...
madan@madan-Lenovo-G570:~/madan$ ./a.out 123
sum:36
not an armstrong number..
Program to convert a number into HexaDecimal format
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
main()
{
int i=0,n,count=0,a[10];
char ch[10];
printf("enter a number\n");
scanf("%d",&n);
while(n>0)
{
a[i]=(n%16);
n=n/16;
i++;
count++;
}
for(i=count-1;i>=0;i--)
{
if(a[i]==10)
ch[i]=a[i]+55;
if(a[i]==11)
ch[i]=a[i]+56;
if(a[i]==12)
ch[i]=a[i]+57;
if(a[i]==13)
ch[i]=a[i]+58;
if(a[i]==14)
ch[i]=a[i]+59;
if(a[i]==15)
ch[i]=a[i]+60;
printf("%c",ch[i]);
printf("\n");
}
}
OutPut:
enter a number
12
E
Program to find square and cube of a number
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int sum(int a,int b)
{
int sum1;
sum1=a+b;
return sum1;
}
int squ(int c)
{
int square;
square=(c*c);
return square;
}
int cube(int s)
{
int cube;
cube=(s*s*s);
return cube;
}
main()
{
int s,c;
int a=10,b=20,cu=cube(s=squ(c=sum(a,b)));
printf("sum=%d square=%d cube=%d ",c,s,cu);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
sum=30 square=900 cube=729000000
Program to view the difference between data types
#include<stdio.h>
#include<string.h>
main()
{
int a=10,b=22,c;
printf("a=%d b=%d c=%d\n",a,b,c=(a+b));
printf("a=%d b=%f c=%f\n",a,b,c=(a+b));
printf("a=%f b=%f c=%f\n",a,b,c=(a+b));
printf("a=%d b=%d c=%d\n",a,b,c=(a+b));
}
OutPut:
enter values for a and b
10
20
a=10 b=20 c=30
a=10 b=0.000000 c=0.000000
a=0.000000 b=0.000000 c=0.000000
a=10 b=20 c=30
Program to find Factorial of a number
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void factorial(int n)
{
int fact=1,i;
for(i=1;i<=n;i++)
fact=fact*i;
printf("factorial of %d =%d\n",n,fact);
}
main(int argc,char **argv)
{
int i,num;
for(i=1;i<argc;i++)
{
num=atoi(argv[i]);
factorial(num);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 5
factorial of 5 =120
Program to print Fibonacci series
#include<stdio.h>
#include<string.h>
main()a
{
int a=0,b=1,c=0,n;
printf("enter value for n");
scanf("%d",&n);
printf("%d %d ",a,b);
while(n>c)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
printf("\n");
}
OutPut:
enter value for n:
9
0 1 1 2 3 5 8 13
Program to find the string is Armstrong or not
#include<stdio.h>
#include<string.h>
void armstr(int);
main()
{
int min,max,i=0,n,a;
printf("enter min and max range...\n");
scanf(" %d %d",&min,&max);
for(i=min;i<=max;i++)
{
//n=min;
armstr(i);
//temp=n;
}
}

void armstr(int n)
{
//printf("n=%d\n",n);
int temp,rem=0,sum=0;
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(sum==temp)
printf("armstrong:%d\n",sum);
}
OutPut:
enter min and max range...
100
500
armstrong:153
armstrong:370
armstrong:371
armstrong:407
Program to convert decimal number to any other number format(hex/octal/binary)
#include<stdio.h>
#include<string.h>
main()
{
int n,i=0,a[10],choice=0,count=0;
printf("enter any number...\n");
scanf("%d",&n);
printf("enter ur choice>\n 1:decimal \n2:octal \n3:hexadecimal \n4:binary\n");
scanf("%d",&choice);
if(choice==1)
printf("decimal=%d\n",&n);
else if(choice==2)
{
while(n>0)
{
a[i]=(n%8);
n=(n/8);
i++;
count++;
}
for(i=count-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
else if(choice==3)
{
while(n>0)
{
//i=0;
a[i]=(n%16);
n=(n/16);
i++;
count++;
}
for(i=count-1;i>=0;i--)
printf("%d",a[i]);
printf("\n");
}
else if(choice==4)
{
int bit=31;
loop:
while(bit>=0)
{
n&(1<<bit)?printf("1"):printf("0");
bit--;
goto loop;
}
printf("\n");
}
else
printf("enter correct choice...\n");
}
OutPut:

enter any number...
144
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
2
220
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
100
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
3
64
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
100
enter ur choice>
 1:decimal 
2:octal 
3:hexadecimal 
4:binary
4
00000000000000000000000001100100
Program to print a number is palindrome or not
#include<stdio.h>
#include<string.h> 
main()
{
int n,i,result=0,num;
printf("enter any number...\n");
scanf("%d",&num);
printf("number before reverse..%d\n",num);
n=num;
while(n>0)
{
result=result*10;
result=result+(n%10);
n=n/10;
}
if(num==result)
printf("number is palindrome\n");
else
printf("not a palindrome number...\n");
}
OutPut:
enter any number...
121
number before reverse..121
number is palindrome
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any number...
123
number before reverse..123
not a palindrome number...
Program to find square root of a number
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i;
printf("enter n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
if(i*i==n)
printf("square root of %d is %d\n",n,i);
}
OutPut:
enter n
4
square root of 4 is 2
Program to find sum of digits of a number
#include<stdio.h>
#include<string.h>
/////////////program to print sum of digits///////////
void sumdigit(int n)
{
int sum=0,rem=0;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum of digits is %d\n",sum);
sum);
}


main(int argc,char **argv)
{
int n,sum=0,rem=0,i,result=0;
for(i=1;i<argc;i++)
{
result=result+sum;
n=atoi(argv[i]);
sumdigit(n);
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 123
sum of digits is 6
madan@madan-Lenovo-G570:~/madan$ ./a.out 14356
sum of digits is 19
Program to swap two numbers
#include<stdio.h>
main()
{
int C,D,temp;
printf("enter values for C and D\n");
scanf("%d%d",&C,&D);
C=(C+D);
D=(C-D);
C=(C-D);
//temp=C;
//C=D;
//D=temp;
printf("c=%d d=%d\n",C,D);
}
OutPut:
enter values for C and D
10
20
c=20 d=10
Program to find Average of numbers
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5,total=0;
float average;
printf("enter marks in 5 subjects\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
if(m1&&m2&&m3&&m4&&m5<=50)
{
total=(m1+m2+m3+m4+m5);
average=((m1+m2+m3+m4+m5)/5);
printf("Total:%d Average:%f\n",total,average);
}
else
printf("enter currect marks\n");
}
OutPut:
enter marks in 5 subjects
43
54
32
33
22
Total:184  Average:36.000000
Program to find Area & Perimeter of a circle
//#define pi=3.14;
#include<stdio.h>
main()
{
float l,b,r,arear,perr,areac,perc,pi=3.14;
printf("enter length, breadth of rectangle\n");
scanf("%f%f",&l,&b);
arear=(l*b);
perr=2*(l+b);
printf("area=%f perimeter=%f\n",arear,perr);
printf("enter radius of circle\n");
scanf("%f",&r);
areac=(pi*r*r);
perc=2*pi*r;
printf("area of circle=%f perimeter of circle=%f\n",areac,perc);
}
OutPut:
enter length, breadth of rectangle
1
2
area=2.000000 perimeter=6.000000
enter radius of circle
4
area of circle=50.240002 perimeter of circle=25.120001
Program to print octal,decimal,hexa decimal formats
#include<stdio.h>
main()
{
int n;
printf("enter value for n\n");
scanf("%d",&n);
printf("n=%d n=%x n=%o\n",n,n,n);
}
OutPut:
enter value for n
45
n=45 n=2d n=55
Program to print integer equivalent of character
#include<stdio.h>
main()
{
int n;
printf("enter any integer\n");
scanf("%d",&n);
printf("n=%d n=%c\n",n,n);
}
OutPut:
enter any integer
123
n=123 n={
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
45
n=45 n=-
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
56
n=56 n=8
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter any integer
98
n=98 n=b
Dynamic Memory allocation concept Related to 1D array
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int a[4]={0},*p,i;
p=malloc(4*sizeof(int));
for(i=0;i<4;i++)
scanf("%d",&a[i]);
for(i=0;i<4;i++)
printf(" %d ",a[i]);
}
OutPut:
1
2
3
4
 1  2  3  4
Implementation of atoi function
#include<stdio.h>
int myatoi(const char *string);
int main(int argc, char* argv[])
{
int i;
for(i=1;i<argc;i++)
  printf("\n%d\n", myatoi(argv[i]));
//  getch();
 // return(0);
}
int myatoi(const char *string)
{
    int i;
    i=0;
    while(*string)
    {
         i=(i<<3) + (i<<1) + (*string - '0');
         string++;
         // Dont increment i!
    }
    return(i);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1234

1234
Program to print Binary Equivalent of a number
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int num,bit=0;
printf("enter a number\n");
scanf("%d",&num);
l1:
num&(1<<bit)?printf("1"):printf("0");
bit++;
if(bit<=31)
goto l1;
printf("\n");
}
OutPut:
enter a number
12
00110000000000000000000000000000
(for big endian machine)
Program to print strings through command line
#include"myHeader.h"
main(int argc,char **argv)
{
int i;
for(i=0;i<argc;i++)
printf("%s\n",argv[i]);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out 1223 1223
1223
1223
Pointers with address references example
#include"myHeader.h"
main()
{
int a=10;
char *p=&a;
int *p1=&a;
float *p2=&a;
printf("%d\n",*p);
printf("%d\n",*p1);
printf("%d\n",*p2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
10
10
2147483645
Program to read a file by using fgets
#include"myHeader.h"
main(int argc,char **argv)
{
char ch[20];
FILE *fp;
if(argc!=2)
printf("enter correct no of arguments\n");

fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("no file exists\n");
return;
}
while((fgets(ch,20,fp)))
printf("%s",ch);
fclose(fp);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter correct no of arguments
no file exists
madan@madan-Lenovo-G570:~/madan$ ./a.out 1.c
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}
fseek,ftell importance through program
#include"myHeader.h"
main()
{
int pos;
char str[20]="abcdefgh";
FILE *fp;
fp=fopen("madan","r");
if(fp==NULL)
{
printf("no data\n");
return;
}
fseek(fp,4,0);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,3,1);
pos=ftell(fp);
printf("%c ",str[pos]);
fseek(fp,-1,1);
pos=ftell(fp);
printf("%c ",str[pos]);
//fseek(fp,0,0);
//pos=ftell(fp);
rewind(fp);
printf("%c ",str[pos]);
fclose(fp);
}
OutPut:
e h g g
Read More