Monday 16 September 2013


Q Find the output of the following programs :

          (i)
                             #include<stdio.h>
                             #include<conio.h>
                             void main()
                             {
                                      int a=5;

                                      int *b;

                                      b=&a;

printf("\nValue of a = %d" ,a);   ==>       Value of a = 5

printf("\nValue of a = %d" ,*(&a)); ==> Value of a = *(1000) ==> 5
printf("\nValue of a = "%d ,*b);   ==> Value of a = *b ==>*1000==>5

printf("\nAddress of a = %u" ,&a); ===> Address of a = 1000

printf("\nAddress of a = %u" ,b); ===> Address of a = 1000

printf("\nValue of b = %u" ,&a);===> Value of b = &a===> Address of a = 1000

printf("\nValue of b = %u" ,b); ===> Value of b = b===> Address of a = 1000


printf("\nAddress of b = %u",&b);===> Address of b =&b===> Address of b = 2000


          getch();

}

                             output :

                                



 (ii)

          #include<stdio.h>
          #include<conio.h>
                  
          void main()
          {
                   int a=5; /* Integer type variable */

                   int *b=&a; /* Pointer to an integer */              

                   int **c=&b; /* Pointer to a Pointer to an Integer */

                   int ***d=&c; /* Pointer to Pointer to Pointer to an Integer */



                   printf("\n%d%u%u%u",a,b,c,d);


                   printf("\n%d %d %d ",a+*b;*b+**c,*b+**c+***d);

                   getch();
}

output :



5 1000 2000 3000

a+*b ==>   5 + *(1000) ==> 5+5 = 10

*b+**c ==> *1000+ **2000 ==> 5 + *1000 ==> 5+5 = 10

*b+**c+***d===> *1000+**2000+***3000

                   5+*1000+**2000
                   5+5+*1000
                   5+5+5
                   15

(iii)

          #include<stdio.h>
          #include<conio.h>
          void main()
          {
                   int *p=4000;

                   printf("\n p=%u " , p);

                   p=p+1;

                   printf("\n p+1=%u",p);

          }

          %u is the format specifier used for printing the address . The address is always 16 bit and %d will point to 15 bit and they consider the one bit for storing the sign bit. And in case of %u, that is , unsigned all the 16   bits are used for storing the value.

          

          When ever we increament in the address , it will points to the next location  of that type.
          p=p+1=4000+1=4002

          As , each integer is of 2 bytes

(iv) main()
          {
                   char *c=4000;
                   int *i=4000;
                   float *f=4000;
                   double *d=4000;
                   long *l=4000;
                  
                   printf("\nc=%u , c+1=%u",c,c+1);
                   printf("\ni=%u , i+1=%u",i,i+1);
                   printf("\nf=%u , f+1=%u",f,f+1);
                   printf("\nd=%u , d+1=%u",d,d+1);
                   printf("\nl=%u , l+1=%u",l,l+1);
          }

===================================================
          Datatype                                          Size in Bytes
===================================================
          char                                                            1 byte
          int                                                               2 bytes
          float                                                  4 bytes
          double                                                       8 bytes
          long                                                  4 bytes
===================================================

So,the result is ,
          c=4000      , c+1=4001

          i=4000 , i+1= 4002

          f=4000 , f+1 = 4004

          d =4000 , d+1 = 4008


          l = 4000 , l+1=4004

No comments:

Post a Comment