C Programs

C PROGAMS WITH OUTPUT

----------------------------------------------------------------------------
main()
   {
      printf("%u" , main());
   }
//this program tells us that main() is an function
// so this is an infinite loop program.
----------------------------------------------------------------------------
void main(void)
{
        int y,z;
        int x=y=z=10;
        int f=x;
        float ans=0.0;
        f *=x*y;
        ans=x/3.0+y/3;
        printf("%d %.2f",f,ans);//1000 6.33
}
----------------------------------------------------------------------------
{
            clrscr();
            int var1,var2,var3,minmax;
            var1=5;
            var2=5;
            var3=6;
            minmax=(var1>var2)?(var1>var3)?var1:var2:(var2>var3)?var2:var3;
            printf("%d\n",minmax);//6
}
/*consider us  (var1>var2)? results as (5>6)?5:5 ==>5 and (5>6>)?5:6 ==> 6
the result is   (5>5)?5:6 ==>6
so the answer is 6


----------------------------------------------------------------------------
main()
{
clrscr();
char *s="hello world";
printf ("%%s",s);//%s
printf ("\n%%%s",s);//%hello wrold
printf ("\n%a%s",s);//%a%s
printf ("\n%%.*%s",s);//%.*hello world
}
----------------------------------------------------------------------------
void main()
{
clrscr();
int i=5.3;
printf("%d %x",0x9,i);//here 9 and 5 will be printed.
}
----------------------------------------------------------------------------
void main()
{
clrscr();
int a,d;
scanf("xyz abc ABC 345" "% *[a-z A-Z]lf",&a);
printf ("%d",a);
scanf("partha","sarathy",&d);
printf ("\n%d",d);
}
//both don't give error they will get input and some garbage values are
//printed as output.
----------------------------------------------------------------------------
int i=5;
void main()
{clrscr();
{i=9;
printf ("%d",i);}//9
printf ("\n%d",i);//9
}
/*
if we use int i=9;then o/p will be 9 with in loop and 5 for global one
9 5 will be printed.*/

----------------------------------------------------------------------------
void main(){
const int x=8;
int c;
c=x++;
printf ("%d",c);
}
// here the program gives a error as we can't change the value of x
//throughout the program.
// error :can't modify a const object.
----------------------------------------------------------------------------
main()
{
            char *s1="Ramco";
            char *s2="Systems";
            printf("%x",&s2);//fff2 address of s2
            ++s2;
            ++s2;
            printf("\n%s",++s2);//o/p is 'tems'
            printf("\n%x",&s2);//same 'fff2'
            printf("\n%x",&(*(s2)));// 'b3'
            getch();
}
/* important :
   ---------
s2 ie. the string "systems" will have a address fff2 (ie.&s2) it won't change
when we ++s2.

but 's' 'y' 's' and ... will have a its own addresses b0 b1 b3 ...
we are doing ++s2 three times so pointer goes from b0 to b3
and b3 is printed when &(*(s2)).*/
----------------------------------------------------------------------------
main()
{
            char *s1="Ramco";
            char *s2="Systems";
            *s1=*s2;//the first char of s2 'S'will be assigned to first
//char of s1 ie.'R' so the o/p will be 'Samco'
            printf("%s",s1);
}
----------------------------------------------------------------------------
main()
{
            clrscr();
            char *t;
            char *s1="Ramco";
            char *s2="Systems";
            t=s2;//the string in s2 is completely compied to t
            printf("%s",t);//Systems
            t++;
            printf("\n%s",t);//ystems
            printf("\n%c",*t);//y
            printf("\n%s",*t);//wreid charachers are printed
            getch();
}
----------------------------------------------------------------------------
main()
{
            char *ptr = "Ramco Systems";
            (*ptr)++;//here the first char of *ptr is added to 1
//ie. R+1 in converting to ASCII we 'S'so the o/p will be Samco Systems
            printf("%s\n",ptr);
            ptr++;//here the address of the *ptr moves to next position so the
//it points to second char.therefore the o/p will be amco Systems.
            printf("%s\n",ptr);
}
//we can't print directly as printf("%s\n",(*ptr)++);
//output gives some abnormal program termination.
RAMCO SYSTEMS -----------------------------------------------------------------------------
main()
{
            char *s1="Ramco";
            char *s2="Systems";
            char *t=" ";
            s2++;////here the pointer of the *s2 moves to next position so the
//it points to second char. 'y'
            *t=*s2;//it is assigned to t
            printf("%s",t); // o/p :y
}



----------------------------------------------------------------------------
//this program is without using pointers and dynamic memory allocation
main()
{
            char p1[20];
            char p2[20];
            strcpy(p1,"Ramco");
            strcpy(p2,"Systems");
            strcat(p1,p2);
            printf("%s",p1);//RamcoSystems
}
STRCPY PROGRAMS-------------------------------------------------------------
#include<stdlib.h>
main()
{
            char *p1;
            char *p2;
            p1=(char *) malloc(25);
//          p2=(char *) malloc(25 * sizeof(char)); doubt
            printf("%d",sizeof(p2));
            strcpy(p1,"Ramco");
            strcpy(p2,"Systems");
            strcat(p1,p2);
            printf("\n%s",p1);//RamcoSystems
}

----------------------------------------------------------------------------
/*PROGRAM 1
void main()
{
char '-'=45;//declaration terminated incorrectly
char '/'=47;
printf("%d/n",'-','-','-','-','/','/','/');
}//this is a error program.*/
//PROGRAM 2
void main()
{
int i;
   i=1;
   i=i+2*i++;//here first i will be substituted as 1 for all then
//presedence goes to 2*i so result will be 2 and i will be incremented
// by 1 in memory.so result will be 1+2=3 and plus 1 in memory so o/p is 4.
   printf("%d",i);
}
/*PROGRAM 3
void main()
{
printf ("%d",5);//5
printf ("\n%f",5.82);//5.820000
printf ("\n%g",5.82);//5.82
printf ("\n%u",-1);//some wreid number :65535 is got as answer
}*/
----------------------------------------------------------------------------
void main()
{
double x = 1/2.0 + 1/2;
printf("x=%.2f\n", x);//0.50
}
EXPERSSIONS------------------------------------------------------------------
//PROGRAM 1
/*
main()
{
int x,j,k;
j=k=6;x=2;
x=j*k;
printf("%d", x); // o/p is 36
}
PROGRAM 2
main()
{
int i=20,k=0,j;
for(j=1;j<i;j=1+4*(i/j))
{
k+=j<10?4:3;
}
printf("%d", k); // o/p is 4
}*/
//PROGRAM 3
main()
{
int Y=10;
if(Y++>9&&Y++!=10&&Y++>10) //here all the conditions are true
printf("........ Y");// this is the o/p
else
printf("....  ");
}
i++-------------------------------------------------------------------------
void main()
{
  clrscr();
  int i=1;
 printf("%d %d %d %d",i,i++,i--,i++);
}
/*printf is executed from the reverse side.
i++ 1 is printed 2 is in memory.
1-- 2 is printed 1 is in memory.
i++ 1 is printed 2 is in memory.
i   2 is printed.
now printing in order
2 1 2 1
IF ---------------------------------------------------------------------------
void main()
{
int no_fish;
            no_fish=1;
            if (no_fish==1)
            printf("The water was to warm\n");
            else;
            printf("The wates were all fished out\n");
}
/*both printf will be printed as o/p since else is terminated by ;
if we have

            if (no_fish==1);
            printf("The water was to warm\n");
            printf("The wates were all fished out\n");
this also results as above */
----------------------------------------------------------------------------
main()
{
            int c=3;
            switch(c)
            {
            case 2: c=3;
            case 3: c=4;
            case 4: c=5;
            case 5: c=6;
            default: c=7;
            }
printf ("%d",c);
}//here no break is given so the control goes to next case 4 and on..
//finally c=7 is taken and printed.
----------------------------------------------------------------------------
main()
{
int i;
switch(i)
 {
printf ("%d",i);
case 6:printf ("%d",i);break;
default:printf ("%d",i);
 }
}//some wreid number is got as o/p
//there is a warning as unreachable code.
----------------------------------------------------------------------------
void fun();
void main()
{
int i1;
            switch(i1)
        {case 1: goto lure;break;
         case 2: printf("This is second choice");break;
         default: printf("This is default choice");
            }
}
void fun()
{
lure: printf("This is unconditional jump");
}
//this gives error as the label lure is out of the function main
----------------------------------------------------------------------------
void main()
{
int i,j=6;
            switch(i)
            {
                        case 1: printf("This is first choice");
                                    break;
                        case j: printf("This is second choice");
                                    break;
                        case 1+2+4: printf("This is the third and last choice");
                                    break;
            }
}
//here their is a error in case j: ie.in case statement should have only
//constant values. the error is 'constant experession required'.
----------------------------------------------------------------------------
void main()
{
int i = 4;
switch (i)
{
   default:
      ;
   case 3:
      i += 5;
      if ( i == 8)
      {
         i++;
         if (i == 9) break;
             i *= 2;
      }
      i -= 4;
      break;
   case 8:
      i += 5;
      break;
}
printf("i = %d\n", i);//5
}
SWITCH ----------------------------------------------------------------------
void main()
{
char ch='A';
while(ch<='F')
 {
switch(ch)
   {
            case'A':case'B':case'C':case'D':ch++;continue;
//so continue will take the control to while until ch becomes 'E'
//so only FG will be printed.
            case'E':case'F':ch++;
   }
   putchar(ch);
 }
getch();
}
----------------------------------------------------------------------------
main()
      {clrscr();
        char *p='a';
        int *i=100/*p;//error it takes as comment line /*
                        printf ("%d",*i);
     }
COMMENT LINE ----------------------------------------------------------------
void main()
{
int a=1,b=2,c=3,*pointer;
   pointer=&c;
   a=c/*pointer;
//here actually we give c divided by *pointer but compiler will take it as
//the comment line /* and remaning will not be considered. so this gives
//error while running.
   b=c;
   printf("a=%d b=%d",a,b);
}
FUNCTIONS --------------------------------------------------------
char *f()
 {
  char s[10];
  strcpy(s,"goodbye");
//  printf ("%s",s);
 }
void main()
{
 char  *f();
 printf("\n%c",*f()); //here the output is 'g'
}
//first char of the fn is returned.
----------------------------------------------------------------------------
int fun (int);
void main()
{
int n;
n=5;
fun(n);
}
int fun( int n)
{
int i;
for(i=0;i<=n;i++)
 {
 printf ("\n%d %d",i,n);//infinite loop printing 0 5
 fun(n-i);
 }
printf(" well done");
}
RECURSIONS ------------------------------------------------------------------
//example for recursion
int sum(int n);
void main()
{
int n=5;
printf("\n%d",sum(n));
}
int sum(int n)
{
printf ("%d",n);
if(n<=1)return n;
else return(n+sum(n-1));
//n value will go from from 54321 and their sum is 15.
 }

----------------------------------------------------------------------------
#define prn(a) printf("%d",a)
#define print(a,b,c) prn(a), prn(b), prn(c)
#define max(a,b)  (a<b)? b:a
main()
{
int x=1, y=2;
print(max(x++,y),x,y);//2 2 2
print(max(x++,y),x,y);//3 4 2
getch();
}
/*firstly 1++ < 2 ? 2 : i++ ==> so the condition is true it returns 2
              1 and
              2 in mem. so x will be 2 and y=2
secondly 2++  >  2  ?  2  :  i++  ==> condition is false
             2 and             3 and
             3->mem            4 in mem.
so the return will be 3 and x will be 4,y=2;*/
----------------------------------------------------------------------------
#define scan "%s string"
void main()
{
printf(scan,scan,scan,scan,scan);
}
/*here the output is %s string string

if we define as #define scan "string"
then the output is 'string' only once it is printed.
*/
----------------------------------------------------------------------------
#define scan "%s string"
void main()
{
printf(scan,scan,scan,scan,scan);
}
/*here the output is %s string string

if we define as #define scan "string"
then the output is 'string' only once it is printed.
*/
----------------------------------------------------------------------------
# define min(a,b) a+b
void main()
{
clrscr();
int a=3,b=7,c,d=4,e=5,f;
c=min(d,e);
f=min(a,b);
printf("c=%d\n",c);//9
printf("c=%d\n",f);//10
}
----------------------------------------------------------------------------
# define min(a,b) a+b
void main()
{
clrscr();
int a=3,b=7,c,d=4,e=5,f;
c=min(d,e);
f=min(a,b);
printf("c=%d\n",c);//9
printf("c=%d\n",f);//10
}


----------------------------------------------------------------------------
#define swap1(a,b) a=a+b;b=a-b;a=a-b;
void swap2(int *a,int *b);
void main()
{
            int x=5,y=10;
            swap1(x,y);
            printf("%d %d using define preprocessor st. \n",x,y);//10 5
            swap2(&x,&y);
            printf("%d %d by passing values to fn.using pointers \n",x,y);//5 10
}
//in preprocesser also we can pass & return two values as like pointers
//we are using pointers since we can pass & return two values
void swap2(int *p1,int *p2)
{
            int temp;
            temp=*p1;
            *p1=*p2;
            *p2=temp;
            return;
}
DEFINE ----------------------------------------------------------------------
#define MAN(x,y) (x)>(y)?(x):(y)//;printf ("\n%d %d",x,y)
void main()
{
int i=10,j=5,k=0;
k=MAN(i++,++j);
printf("\n%d,%d,%d",i,j,k);//12 6 11
}
/*here i++ and ++j is copied to man(i++ ++j)
ie.(i++)  >  (++j)   ?   (i++)   :  (++j)
   10 and      6          11 and
   11 is in               12 is in
   memory                 memory
so 11 is returned to k and
   12 is in i
   06 is in j*/
IF --------------------------------------------------------------------------
void main()
{
clrscr();
int a=10,b=5,c=3,d=3;
if((a>b)&&(c==d++))
printf("%d %d %d %d", a,b,c,d);
//if both the conditions are true,only then d will be incremented by 1
//so here output is 10 5 3 4
else printf("%d %d %d %d", a,b,c,d);
//if in if we give if((a<b)&&(c==d++)) , here first condition is false
//and second is true so  d will not be incremented.control goes to else
//and output will be as 10 5 3 3
}
----------------------------------------------------------------------------
void main()
{
char *a[2];
*a[0]=7;
*a[1]=5;
printf ("%d",&a[1]);//here the value is -12
printf ("\n%d",a);// this is similar to &a[0],here the value is -14
printf("\n%d",&a[1]-a);// a is an character quantity the o/p is 2/2=1.
}

----------------------------------------------------------------------------
void main()
{
char *a[2];
*a[0]=7;
*a[1]=5;
printf ("%d",&a[1]);//here the value is -12
printf ("\n%d",a);// this is similar to &a[0],here the value is -14
printf("\n%d",&a[1]-a);// a is an character quantity the o/p is 2/2=1.
}

ARRAY -----------------------------------------------------------------------
main()
{
int a[] ={ 1,2,3,4,5,6,7};
char c[] = {'a','x','h','o','k'};
printf("\n%d\t %d ", (&a[3]-&a[0]),(&c[3]-&c[0]));
}
//when we print &a[3] it gives -24 similarly for &a[0] it gives -30
//so the difference between both hexadecimal numbers is 6
//a[3] is stored at the address which is 6 bytes beyond the address of a[0]
//since integer quantity occupies 2 bytes the diff. bet then is 6/2 =3.
//similarly for char as it occupies 1 byte only.
//so the ans is   3      3
----------------------------------------------------------------------------
void main()
{
int x[] = { 1, 4, 8, 5, 1, 4 };
int *ptr, y;
printf ("%d",x);//-24
ptr  = x + 4;
printf ("\n%d",ptr);//-16
y = ptr - x;
printf ("\n%d",y);//4
}
----------------------------------------------------------------------------
void main()
{
    char p[]="string";
    char t;
    int i,j;
    for(i=0,j=strlen(p)-1;i<j;i++)
    {
    t=p[i];
    p[i]=p[j-i];
    p[j-i]=t;
    printf("\n%s",p);//nothing will be printed
    }
    printf("%s",p);//nothing will be printed
}
/* if we put j=strlen(p)-1 then p inside for will be executed 5 times
gtrins gnrits gnirts gnrits gtrins
and gtrints is printed as output*/
----------------------------------------------------------------------------
struct s1 {int i; };
struct s2 {int i; };
void main()
{
struct s1 st1;
struct s2 st2;
st1.i =5;
st2 = st1;
printf(" %d " , st2.i);
}
//here there are two structures s1 and s2 coresponding to their variable
//names st1 anf st2.so here values from one structure to another structure
//cannot be copied.this is an error program.
----------------------------------------------------------------------------
main()
{
struct emp{
            char emp[10];
            int empno;
            float sal;
              };
struct emp member = {"TIGER",4,5.7};
printf(" %d %f", member.empno,member.sal);
getch();
}
//the size of the emp[] is not given so this program gives error.
//if we give emp[10] the output will be 4 5.700000
//if we define the structure as emp member ={"TIGER"}
//then think of the output,it won't give error.
//it gives 0 0.000000
----------------------------------------------------------------------------
main()
{
struct emp{
            char emp[10];
            int empno;
            float sal;
              };
struct emp member = {"TIGER",4,5.7};
printf(" %d %f", member.empno,member.sal);
getch();
}
//the size of the emp[] is not given so this program gives error.
//if we give emp[10] the output will be 4 5.700000
//if we define the structure as emp member ={"TIGER"}
//then think of the output,it won't give error.
//it gives 0 0.000000
----------------------------------------------------------------------------
//structures and pointers
#include<malloc.h>
void main(void);
typedef struct
{
            int i;
            char c;
            long x;
} NewType;
void main(void)
{
            NewType *c;
            c=(NewType *)malloc(sizeof(NewType));
            c->i=100;
            c->c='C';
            (*c).x=100L;
            printf("(%d,%c,%4d)",c->i,c->c,c->x);(100,C,100)
}
//see schaum's series book p-357 for details
----------------------------------------------------------------------------
void main()
{
struct dclass//if we use class here it will give error since it is a keyword
{int a;
float b;
char string[5];
}str;
printf ("%d",sizeof(dclass));//11
printf ("\n%d",sizeof(str));//11
}
STRUCTURES ------------------------------------------------------------------
struct s1{int i; }st1,st;
void main()
{
st1.i =5;
st=st1;
printf(" %d " , st.i);
}
//single structure with two variable names st1 and st
//st1 value can be copied directly to st by st=st1.
//output is 5.
----------------------------------------------------------------------------
main()
{
int i,j;
int mat[3][3] ={ // matrix can be represented as below also.
                          {1,2},
                          {4,5},
                          {7,8}
                        };
for (i=2;i>=0;i--)
 for ( j=2;j>=0;j--)
printf("%d" , *(*(mat+j)+i));
getch();
}
/*matrix is like  1 2 0   Default the third column is taken as zeros.
                          4 5 0
                          7 8 0
in two for loops we see
j  i  value
-----------
2  2     0
1  2     0
0  2     0
2  1     8
1  1     5
0  1     2
2  0     7
1  0     4
0  0     1
so the output will be 000852741*/
----------------------------------------------------------------------------
main()
{
int i[]={1,2,3,4,5};
printf ("%d",3[i]);//4 this is similar to i[3]
printf ("\n%d",*(1+i));//2 this is similar to *(i+1);
}
//we can say that it satisfies commutative law.
----------------------------------------------------------------------------
void main(void);
void main(void)
{
            char num[5][6]={"Zero","One","Two","Three","Four"};
            printf ("%s",(*(num+2)+0));//Two
            printf("\n%s is %c",&num[4][0],num[0][0]);//Four is Z
}
/*the matrix num[5][6] is takes as

   col 0  1  2  3  4  5  6
row 0  Z  e  r  o
    1  O  n  e
    2  T  w  o
    3  T  h  r  e  e
    4  F  o  u  r
this st. (*(num+2)+0) is similar to &num[2][0].ie.second rwo is printed
with out pointers we can print as &num[4][0] ==> Four
note that & is given before num[4][0].
TWO DIMENSIONAL ARRAYS ------------------------------------------------------
main()
{
int i,j;
int mat[3][3] ={1,2,3,4,5,6,7,8,9};
for (i=2;i>=0;i--)
 for ( j=2;j>=0;j--)
printf("%d" , *(*(mat+j)+i));
getch();
}
/*matrix is like  1 2 3
                          4 5 6
                          7 8 9
in two for loops we see
j i  value
2 2       9
1 2       6
0 2       3
2 1     8
1 1     5
0 1     2
2 0     7
1 0     4
0 0     1
so the output will be 963852741



UNIONS ---------------------------------------------------------------------
void main()
{
clrscr();
union test
{
    int a;
    int b;
} testUnion;

testUnion.a = 7;
testUnion.b = 12;
printf( "%d\n", testUnion.a + testUnion.b );//24
}
----------------------------------------------------------------------------
void main()
{
char a[]="hellow";
char *b="hellow";
char c[6]="hellow";
printf("%s %s %s ",a,b,c);
printf("\n%d %d %d",sizeof(a),sizeof(b),sizeof(c));
}
/*in first char, 'hellow' string length is 6 and '\o'(null) is added with it
so the length is 7

in second char is the pointer string so it takes the whole string 'hellow'
as one and '\o' is added with it so the length is 2

in third char,the index of c is 6 so the length is 6
in last hellow we see some weird characters,it is due to absence of '\o'
if you put c[7] then it will be okay*/


----------------------------------------------------------------------------
void main()
{
int short x[5][7];
printf ("%d",sizeof(x));
}
   union{ 
                int x;
                char y;
                struct {
                char x;
                char y;
                int xy;}p;
                }q;
SIZEOF ---------------------------------------------------------------------
void main()
{
clrscr();
char *x,y;
int *d[4];
int *i;
float *f;
printf("%d", sizeof *x);//1
printf("\n%d", sizeof x);//2

printf("\n%d", sizeof *i);//2
printf("\n%d", sizeof i);//2

printf("\n%d", sizeof f);//2
printf("\n%d", sizeof *f);//4

printf("\n%d", sizeof y);//1

printf("\n%d", sizeof d);//8 as integer occpies 2 bytes each so 2*4 =8
printf("\n%d", sizeof *d);//2 it takes the first element in array 'd'

printf("\n%d", sizeof "");//1 ie. '\o'is added
printf("\n%d", sizeof "partha");//7 ie. partha + '\o' = 7
}
----------------------------------------------------------------------------
void main()
{
enum days {sun,mon,tue,wed,thu,fri,sat};//actually its as {0,1,2,3,4,5,6)
days day1,day2;
day1 = mon;//it's as day1=1
day2 = thu;//it's as day1=4
int diff = day2-day1;//just gives the diff bet. them
cout <<"Days between  = "<< diff<<endl;//3
if (day1<day2)//can do comparisons
cout<<"Day1 comes before Day2\n";//this will be printed
}

ENUM ------------------------------------------------------------------------
 main()
 {
enum var{ left=10, right, front=100, back};
printf("left is %d, right is %d, front is %d, back is %d",left,right,front,back);
 }
//here the values for variable name var are 10,11,100,101

GENERAL ---------------------------------------------------------------------
int f= 100;
main()
{
float value=10.00;
printf("%g %5g %0.4g %f",value,value,value,value);
}
printf ("%d",f);
//in %5g it leaves 5 gaps and then 10 is printed.
//o/p is 10     10 10 10.000000
----------------------------------------------------------------------------
void main()
{
clrscr();
int v=3, *pv=&v;//this is similar to assigning pv=&v and then printing *pv.
printf(" %d %d ", v,*pv);//the o/p is 3 3
}# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();
int num[]={10,15,5,22,90};
  int *p,*q;
  int i;
  p=num;//the array of num is completely copied to p
  q=num+2;//the address of 2 element of array num ie.5 is copied to q
  *p++;//first p++ is considered and it points to 15 in array p(ie.first ele.)
//if we give as (*p)++ then the value of 10 is incremented by 1 so the
//output will be 11
  printf ("%d",*p);//output will be 15
  printf ("\n%d",*(q+1));//output will be 22.
}----------------------------------------------------------------------------
int *f1();
main()
{
    clrscr();
    int *b=f1();//the value of a is stored in *b
    int c=*b;
printf ("%d",c);//here 5 is printed.
}

int *f1()//this returns an address of some other variable.
            {
            int a=5;
            return &a;
            }
----------------------------------------------------------------------------
void main()
{
            clrscr();
            int a[4]={0,5,10,15};
            int *ptr,i;
            ptr=a; //the array 'a' values are completely copied to pointer ptr.
            *a=*(++ptr)+(*ptr++);
printf ("%d\n",*a);//10 is printed.

for (i=0;i<=3;i++)
 {
 printf ("\n%d",*(a+i));
 }
}

/*if we put *a=*(++ptr)+(*++ptr); 20 is printed .how? (doubt)
similarly if we put  printf ("\n%d",*(ptr+i));  (doubt)


*a is = 5 + 5 = 10
now we are printing the array form 0 to 3
the output
a[0] is 10
a[1] is 5 as the value is altered.
a[2] is 10
a[3] is 15*/
----------------------------------------------------------------------------
void main(void);
void main(void)
{
            clrscr();
            void pa(int *a,int n);
            int arr[5]={5,4,3,2,1};
            pa(arr,5);
}

void pa(int *a,int n)
{
            int i;
            for(i=0;i<n;i++)
            printf("%d\n",*(a++)+i);//55555
}
//the values are pointed as 5+0,4+1,3+2,2+3,1+4
----------------------------------------------------------------------------
void main(void);
void main(void)
{
            clrscr();
            void pa(int *a,int n);
            int arr[5]={5,4,3,2,1};
            pa(arr,5);
}

void pa(int *a,int n)
{
            int i;
            for(i=0;i<n;i++)
            printf("%d\n",*(a++)+i);//55555
}
//the values are pointed as 5+0,4+1,3+2,2+3,1+4
----------------------------------------------------------------------------
#include<process.h>
int bags[5]={20,5,20,3,20};
void main(void)
{
clrscr();
int pos=5,*next();
*next()=pos;
printf("\n%d %d %d",pos,*next(),bags[0]);
}

int *next()
{
            int i;
            for(i=0;i<5;i++)
     {
            printf ("\n%d",i);
            if (bags[i]==20)
            {
            printf ("\n%d",i);
            printf ("\n%d",*(bags+i));
            return(bags+i);}
      }
            printf("Error!");
            exit(0);
}----------------------------------------------------------------------------
# define min(a,b) (a>b)?a:b
int  *scan (int z[2]);
void main()
{
clrscr();
int z[1],*pz; // here decalartion for z can be as *z // also we can't declare as z[]
// this is also not needed z= (int *) malloc(2* sizeof(int));
z[0]=5;z[1]=7;
pz = scan(z);
printf ("the bigest is %d",*pz);//7
}

int *scan (int z[]) // similarly we can have int *scan (int *z)
 {
  int *pf,c;
  c= min(z[0],z[1]);
  pf=&c;
  return(pf);
  }
----------------------------------------------------------------------------
main()
{
int c,i=0;
clrscr();
char *x="string";
char y[]="add";
char *z;
//z=(char *) malloc(sizeof(x)+sizeof(y)+1);//this statement is optional.
strcpy(z,y);
strcat(z,x);
printf("%s+%s=%s",y,x,z);//o/p : add+string=addstring
}
----------------------------------------------------------------------------
main()
{
            clrscr();
            char *p1="Name";
            char *p2;
            p2=(char *)malloc(20);
            while(*p2++=*p1++)
            printf("%s\n",p2);
}
----------------------------------------------------------------------------
int bags[5]={20,5,20,3,20};
void main(void)
{
clrscr();
int pos=5,*next();
*next()=pos;
printf("\n%d %d %d",pos,*next(),bags[0]);
}

int *next()
{
            int i;
            for(i=0;i<5;i++)
     {
            printf ("\n%d",i);
            if (bags[i]==20)
            {
            printf ("\n%d",i);
            printf ("\n%d",*(bags+i));
            return(bags+i);}
      }
            printf("Error!");
            exit(0);
}
----------------------------------------------------------------------------
void printItem (int *list)
{
   printf("element=%d\n", *(list+3));
}

int main()
{
clrscr();
   int ary[3][3] = { {1}, { 5, 10 }, { 4, 5, 6 } };
   printItem ((int *)ary);
   return 0;
}
/*the array will be as
ary[0][0]=1
ary[0][1]=0
ary[0][2]=0

ary[1][0]=5
ary[1][1]=10
ary[1][2]=0

ary[2][0]=4
ary[2][1]=5
ary[2][2]=6

so the value of
*(list)=1 *(list+1)=0 *(list+2)=0 *(list+3)=5 *(list+4)=10 *(list+5)=0
*(list+6)=4 *(list+7)=5 *(list+8)=6

here we can't give as
*(*(list+1)+0)=10 this gives error why?
*/----------------------------------------------------------------------------
void main()
{
clrscr();
char *ptr;
char myString[] = "abcdefg";
ptr = myString;
ptr += 5;
printf ("%s",ptr);//fg
}----------------------------------------------------------------------------
void main()
{
clrscr();
int a[5],*p;
      for(p=a;p<&a[5];p++)
            {
            *p=p-a;
            printf("%d\n",*p);//01234
            }
}
/*firstly a[0] address is stored in p,then it checks a[0]<a[5],internally
it compares the address of both and the resulting true.the value of p
is assigned as a[0]-a[0]=0 so firstly 0 is printed
IIIly  a[1]-a[0]=1; a[2]-a[0]=2; a[3]-a[0]=3; a[4]-a[0]=4 are printed
respectively.
----------------------------------------------------------------------------
void main()
{
int x=15,y=10,m=10,n=18;
if (x<y)
if(m>n)
printf ("partha");
else printf("sarathy");
printf ("\npartha sarathy");
}
//there is two if and one else so inner if will consider the else
//here 15<10, condition is false so it control goes to last and prints
//'partha sarathy' . Try the control swiching by tracing(f7).
----------------------------------------------------------------------------
main()
     {
     char line[80];
     scanf("%[^\n]",line);// '^' is called as circumflex
//any set of characters of 80 length can be got from input and stored in line
     printf("%s",line);
     }
----------------------------------------------------------------------------
void main()
{
char s1[100];
char s2[100];
gets( s1);
printf ("%d",strlen(s1));//if 'abcd' is given --> 4
fgets( s2, sizeof(s2), stdin);
printf ("\n%d",strlen(s2));//if 'abcd' is given --> 5
printf( "%d\n", strlen(s1) - strlen(s2));//-1
}
----------------------------------------------------------------------------
void main()
{
char *string = "This is my String";
string[3] = '8';
printf ("%s",string);//Thi8 is my String.
}----------------------------------------------------------------------------
void main()
{
char x = 'A';
putchar( x );
putchar( x + 1 );
}//o/p:AB will be printed.
POINTER PROGRAMS-----------------------------------------------------------
# define min(a,b) (a>b)?a:b
void main()
{
clrscr();
int a=3,b=7,*pa,*pb,c,*d,z;
pa =&a;
pb = &b;
c= (*pa**pb);
d =&c;
z= min(*pa,*pb);
printf ("the bigest is %d",z);
printf ("\nThe mult of two numbers %d and %d is %d",*pa,*pb,*d);
printf ("\nThe location of %d and %d is %x and %x",*pa,*pb,pa,pb);
printf ("\nThe location of answer %d is %x",*d,d);
}
CHAR ------------------------------------------------------------------------
main()
{
  clrscr();
  char s[6]="AHELLO";
  printf ("%c",s[16]);//nothing will be printed.
   printf ("%s",s[16]);//(null) will be printed.
   printf ("%s",s[6]);//(null) will be printed.
   printf ("%s",s[2]);//abnormal program termination
  }
----------------------------------------------------------------------------
void main()
{
char buf[10]="sarathy";
const char *ptr=buf;
printf ("%s",ptr);
}
/*here we can't put as ptr[10]=buf;
and char *ptr ='a';these are illegal ones
but char *ptr ="a" is a legal one.*/
----------------------------------------------------------------------------
main()
      {
      char *x="String";
      char y[] = "add";
      char *z;
      z=(char *) malloc(1);//we can allocate as like this also.this statement
//is optional.
      strcpy(z,y);
      strcat(z,y);
      printf("%s+%s=%s",y,x,z);//add+string=addadd
      }
FOR -------------------------------------------------------------------------
void main(void)
{
            int i=10;
            for(;i;)
            printf ("%d",i--);//10987654321
//          printf ("%d",--i);//9876543210
//ie 10 numbers will be printed in reverse order
}
/*
            for(;;)
            printf ("%d",i);//10 infinity times

            for(i=0;;)
            printf ("%d",i);//0 infinity times

            for(;i=10;)
            printf ("%d",i);//10 infinity times

            for(i=1;i;)
            printf ("%d",i--);//1 */
----------------------------------------------------------------------------
main()
{
int c,i=0;
for ( c=0;c!=256;c=c+2)
   {
     printf("%d",c);
             i++;
   }
printf("\n%d times the loop is executed",i);
}
/*how many times the loop is executed?.
the loop is executed for 0 to 254 and inctremented by 2
so 256/2 =128 times.*/
----------------------------------------------------------------------------
int i=10;
void main()
{
int i=20,n;
printf("i value is%d",i); //here i will be 20
for(n=0;n<=i;)//if we give like this it will go for a infinite loop
{
printf("\n2nd i value is%d",i);//here i will be 20.for next time it will
/*become again 20,since it gets out from the loop and searches for increment
as loop has no increment it comes again in to loop,so in for st.(ie.in main
program)the i value is 20,so when it comes inside the loop its value will be
20 only.*/
int i=10;
printf("\n3nd i value is%d",i);//here the value of i will change to 10
i++;
printf("\ni value is%d",i);//here the value of i will change to 11
printf("\nn value is%d",n);//n value will be 0 always
}
printf("%d", i);
}//this program is an infinite loop program

//In the same program if we give n++ in the for statement.we get
//incrementing of n from 0 to 20,finally we get i value as 20
----------------------------------------------------------------------------
void main(void);
int k=100;
void main(void)
{
            int a[105];
            int sum=0;
            for(k=0;k<=100;k++)
            *(a+k)=k;
            printf("after for %d",k);
//important :after, for k will be 101
            sum+=a[--k];
            printf("\n%d",sum);//100
}
----------------------------------------------------------------------------
void main()
{
int i;
for(i=0;i<=10;i++,printf("%d",i));//1234567891011
}
/*i will be initially 0 then it checks for condition then i++ be 1 and 1 is
printed  ... lastly i will be 10 then i++ will be 11 and 11 is printed.*/
----------------------------------------------------------------------------
int SumElement(int *,int);
void main(void)
{
            int x[10];
            int i=10;
            for(;i;)
            {
              i--;
              *(x+i)=i;
              printf ("%d",*(x+i));//9876543210
            }
            printf("%d",SumElement(x,10));//45
}
int SumElement(int array[],int size)
{
            int i=0;
            float sum=0;
            for(;i<size;i++)
             {
              printf("\n%f",sum);
             sum+=array[i]; // sum of 9+8+7+6+5+4+3+2+1+0=45
             }
            return sum;
}
----------------------------------------------------------------------------
void main(void)
{
            unsigned int c=3;
            unsigned x=6;
//          scanf("%u",&c);
printf ("%d\n",c||x);//3 or 6 the conditions are true so the result is 1
printf ("%d\n",c&x);//
            switch(c&x)
            {
                        case 3: printf("Hello!\t");
                        case 2: printf("Welcome\t");
                        case 1: printf("To All\t");
                        default:printf("\npartha sarathy");
            }
}
/*  & is an bitwise AND operator,so here c&x:3&6
3 --> 011
6 --> 110
3&6  -----
      010 = 2 this will be printed.obviously case 2 is selected
----------------------------------------------------------------------------
void main(void);
int newval(int);
void main(void)
{
        int ia[]={12,24,45,0};
        int i;
        int sum=0;
        for(i=0;ia[i];i++)
        {
                sum+=newval(ia[i]);
        }
            printf("Sum= %d",sum);//39
}
int newval(int x)
{
            static int div=1;
            return(x/div++);
}
/*when the ia[3] is pointed the condition for  for loop is 0 so it
exits from the loop and sum is printed
the function is executed 3 times for the values 12 24 45
----------------------------------------------------------------------------
main()
{
int i=6;
float f=7.5;
i=f;//this type of conversions are called implicit one.
//i=int(f) this is of explicit but both the results are same.
printf ("%i",i);//7
}
----------------------------------------------------------------------------
main()
{
int i=6;
float f=7.5;
i=f;//this type of conversions are called implicit one.
//i=int(f) this is of explicit but both the results are same.
printf ("%i",i);//7
}
TYPE CASTING ----------------------------------------------------------------
main()
{
int i=6;
float f=7.5;
i=f;//this type of conversions are called implicit one.
//i=int(f) this is of explicit but both the results are same.
printf ("%i",i);//7
}
----------------------------------------------------------------------------
void main()
{
int x=4,y=8;
x^=y;
y^=x;
x^=y;
printf ("the swaped valued with out any temp variable %d %d",x,y);//8 4
}BITWISE OPERATORS ---------------------------------------------------------
void main()
{
int x,y;
x=6^5;
printf ("%d",x);//3
y=6&5;
printf ("\n%d",y);//4
x=6|5;
printf ("\n%d",x);//7
y=10&&10;//
printf ("\n\n%d",y);//1
x=0||0;
printf ("\n%d",x);//0
x=3<<2;
printf ("\n\n%d",x);//12
y=3>>2;
printf ("\n%d",y);//0
}
/* BITEWISE OPERATORS
   ------------------
& - Bitwise AND
^ - Bitwise Exclusive OR
| - Bitwise Inclusive OR
&& - Logical AND
|| - Logical OR

   Bit value   º         Results of
   ---------   º         ----------

 in E1 ³ in E2 º E1 & E2 ³ E1 ^ E2 (XOR)³ E1 | E2
ÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÎÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍÍÍÍÍÍÍØÍÍÍÍÍÍÍÍ
   0   ³   0   º    0    ³    0         ³    0
   1   ³   0   º    0    ³    1         ³    1
   0   ³   1   º    0    ³    1         ³    1
   1   ³   1   º    1    ³    0         ³    1

6^5       6&5      6|5

1 1 0    1 1 0    1 1 0
1 0 1    1 0 1    1 0 1
-----    -----    -----
0 1 1=3  1 0 0=4  1 1 1=7

&& is like multiplication 0&&10 =0;10&&0=0;10&&10=1;logical output.

<< - Bitwise Left shift operators
eg.3<<2 - here binay of 3 is shifted left 2 times.
0000 0011
0000 0110 - shifted once.
0000 1100 - second shift.
 0    12 = output

>> - Bitwise Right shift operators
3>>2- here binay of 3 is shifted right 2 times.
0000 0011
0000 0001 - shifted once.
0000 0000 - second shift.
 0    0 = output
*/
----------------------------------------------------------------------------
void main()
{
int a=5;
if(a=0) printf("Ramco Systems\n");
printf("Ramco Systems\n");
}
/*in if statement a is assigned to zero, so if will take as if(0)
so the control will skip from if and comes to next statement
in output 'Ramco Systems' is printed only once.
similarly if we put as
if(a=3) printf("Ramco Systems\n");
printf("Ramco Systems\n");
the it will consider as if(3), the condition is true
in output 'Ramco Systems' is printed only twice.
IF --------------------------------------------------------------------------
# include <stdio.h>
main()
             {
              int a=0;
              if(a==0) printf("Ramco Systems\n");
              printf("Ramco Systems\n");
             }
/*o/p:RamcoSystems will be printed twice.
if (a=0)is given then RamcoSystems will be printed once only.
GENERAL---------------------------------------------------------------------
main()
{
clrscr();
char b[17];
int x;
for(x=0;x<=17;x++)
  { b[x]=x+'A';// if x=1,the ASCII of 'A' is added with 1 ie 65+1 = 'B'=b[x]
  printf ("\n%c",b[x]);//ABCD...to R ( upto 17)
  }
printf("\n%s",b);//ABCD..R and a garbage character at the end.
}
----------------------------------------------------------------------------
void main()
{
clrscr();
int i=3;
while(i--)//i will decrement for 3 times untill it comes to 0
//thus this loop will be executed there 3 times.
  {
int i=100;
i--;
printf("%d..",i);//so o/p will be 99..99..99..
  }
}

//note: while(0)is given the loop will not be executed.
// while(-3),while(2) ie.while(any number) loop will be executed infinity times.
----------------------------------------------------------------------------
# define infiniteloop while(1)
main()
{
clrscr();
            infiniteloop;
            printf("DONE");
}
/*here this program os similar to
while(1);
printf"DONE");
note that ; is placed after while so program control won't not go from the
while.o/p will be the blank screen.

if we give
while(1)
printf ("DONE");
this is an infinite loop printing DONE.*/
----------------------------------------------------------------------------
void main()
{
clrscr();
char *p="abc";
char *q="abc123";
if (*p==*q)//yes *p value is equal to *q ie. the value of *p is 'abc'
// as the first 3 char of *q is abc, it won't consider other charachers.

printf("%c %c",*p,*q);//o/p is a a
}----------------------------------------------------------------------------
void main()
{
clrscr();
char *p="abc";
char *q="abc123";
if (*p==*q)//yes *p value is equal to *q ie. the value of *p is 'abc'
// as the first 3 char of *q is abc, it won't consider other charachers.

printf("%c %c",*p,*q);//o/p is a a
}----------------------------------------------------------------------------
static int i=5;
void main(void)
{
            clrscr();
            int sum=0;
            do
            {
//            printf ("\n%d",i);
              sum+=(1/i);//              divide error
//          printf ("\n%d",sum);
            }while(0<i--);
printf ("\n\n%d",sum);
}
/*i will be 5 when it first enters the loop then it decreases in while.
in while the value of i will be 5 and i inside the loop will be 4
finally in while the value of i will be 1 and i inside the loop will be 0
so 1/0 will give 'divide error' as output.

in while if we give the condition as while(0<--i);
the output will be 1*/

----------------------------------------------------------------------------
void main(void);
static int i=50;
int print(int i);
void main(void)
{
            clrscr();
        </SPAN