Pages

This blog is under construction

Monday, December 10, 2018

Quiz-1


What will be output if you will compile and execute the following c codes:


1) #include<stdio.h>
    #define x 5+2
      int main(){
      int i;
      i=x*x*x;
      printf("%d",i); return 0;

Output:27

Explanation:

 #define is token pasting preprocessor it only paste the value of micro constant in the program before the actual compilation start. here it will replace the expression i=x*x*x as follows:

 i=5+2*5+2*5+2; hence i= 27


2)   #include<stdio.h>
       int main(){
       char c=125;
       c=c+10;
       printf("%d",c); return 0;
   }

Output: -121

Explanation:

As we know char data type takes 1-byte storage and the range is -128 to 127.
if you will increase or decrease the char variables beyond its maximum or minimum value respectively it will  move in cyclic order:
here when we increase 125 by 10. we will get -121





3)     int main()
         {
          float a=5.2;
          if(a==5.2)
          printf("Equal");
          else if(a<5.2)
         printf("Less than");
         else
        printf("Greater than"); return 0;
        }

Output: Less than

Explanation:

In C default decimal value is considered as double. here 5.2 is double constant in c. In c size of double data is 8 byte while a is float variable. Size of float variable is 4 byte.
It is clear variable a is less than double constant 5.2
Since 5.2 is recurring float number so it different for float and double. Number likes 4.5, 3.25, 5.0 will store the same values in float and double data type.

4)   #include<stdio.h>
       int main(){
       int x;
       for(x=1;x<=5;x++);
        printf("%d",x); return 0;
      }

Output: 6

Explanation:

The body of for loop is optional. here for loop is terminated so the loop will execute until the value of variable x became six and then condition became false and control comes to print statement.

5)     int main(){
        int array[]={10,20,30,40};
        printf("%d",-2[array]); return 0;
       }

Output: -30

Explanation:

In c language,  array[2]=2[array]= *(array+2)= *(2+array) access same value. 

6)    #include<stdio.h>
         int main(){
          int i=4,x;
          x=++i + ++i + ++i;
          printf("%d",x); return 0;
        }

Output: 21

Explanation:

In ++i, ++ is pre increment operator. In any mathematical expression pre increment operator first increment the variable up to break point then starts assigning the final value to all variable.
so here increment the variable i up to break point then start assigning final value 7 to all variable i in the expression.

x=7+7+7


7)    #include<stdio.h>
       #define max 5;
       int main(){
       int i=0;
       i=max++;
       printf("%d",i++); return 0;
      }



Output: Error

Explanation:

It is clear macro constant max has replaced by 5. It is illegal to increment the constant number 5++. Hence compiler will show Lvalue required.


8)   #include<stdio.h>
  void main(){
  int array[3][2][2]={1,2,3,4,5,6,7,8,9,10,11,12};
  printf("%d",array[2][1][0]);
}


Output: 11

Explanation:


array[3][2][2];  denotes array[pages][rows][columns]




hence array[2][1][0] =11





1 comment: