C Tricky programs – Part 2

(1)What will be the output of the following program

 main()

{

            float me = 1.1;

            double you = 1.1;

            if(me==you)

printf(“Yes “);

else

                        printf(” No “);

}

Answer:

               No

Explanation:

                   For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value  represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.

 (2) What will be the output of the following program

main()

{

            char *p;

            printf(“%d %d “,sizeof(*p),sizeof(p));

}

Answer:

                        1 2

Explanation:

                    The sizeof() operator gives the number of bytes taken by its operand. P is a character pointer, which needs one byte for storing its value (a character). Hence sizeof(*p) gives a value of 1. Since it needs two bytes to store the address of the character pointer sizeof(p) gives 2.

Also Read :  C Tricky Programs

 (3) What will be the output of the following program

main()

{

            int i=1;

            switch(i)

             {

                default:printf(“zero”);

                case 1: printf(“one”);

                           break;

               case 2:printf(“two”);

                          break;

              case 3: printf(“three”);

                          break;

              }

}

Answer :

               one

Explanation :

                    The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn’t match.

 (4) What will be the output of the following program

main()

{

int i=5;

printf(“%d%d%d%d%d%d”,i++,i–,++i,–i,i);

}

Answer:

             45545

Explanation:

                   The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the  evaluation is from right to left, hence the result.

Also ReadLooping tricky questions in C language

(5) What will be the output of the following program

#define clrscr() 5

main()

{

clrscr();

printf(“%d\n”,clrscr());

}

Answer:

              5

Explanation:

                    Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.

(6)What will be the output of the following program

main()

{

printf(“%p”,main);

}

Answer:

         Any random address will be printed.

Explanation:

            Function names are just addresses (just like array names are addresses).main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They are printed as hexadecimal numbers.

(7) What will be the output of the following program

enum colors {BLACK,BLUE,GREEN}

 main()

{

 printf(“%d..%d..%d”,BLACK,BLUE,GREEN);

 return(1);

}

Answer:

             0..1..2

Explanation:

                     enum assigns numbers starting from 0, if not explicitly defined.

Also ReadC Operators tricky questions

(8) What will be the output of the following program

main()

{

int i;

printf(“%d”,scanf(“%d”,&i));  // value 10 is given as input here

}

Answer:

             1

Explanation:

                      Scanf returns number of items successfully read and not 1/0.  Here 10 is given as input which should have been scanned successfully. So number of items read is 1.

(9) What will be the output of the following program

main()

{

int i=0;

for(;i++;printf(“%d”,i)) ;

printf(“%d”,i);

}

Answer:

            1

Explanation:

                      before entering into the for loop the checking condition is “evaluated”. Here it evaluates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after the for loop).

Mohit Arora
Follow me