Programming Languages C Objective
Mar 08, 2013

What will be output of following c code?

#include<stdio.h>

char _x_(int,...);

int main(){

    char (*p)(int,...)=&_x_;

    for(;(*p)(0,1,2,3,4); )

         printf("%d",!+2);

    return 0;

}

char _x_(int a,...){

    static i=-1;

    return i+++a;

}

Choose the correct answer:
A) -1
C) 1
D) 1 1
Detailed Explanation

Explanation:

In c three continuous dot represents variable number of arguments.

p is the pointer to the function _x_

First iteration of for loop:

Initial value: Nothing // In c it is optional

Loop condition: (*p)(0,1,2,3,4)

= *(&_x_)(0,1,2,3,4)  // p = &_x_

= _x_(0,1,2,3,4)    //* and & always cancel to each other

= return i+++a

= return i+ ++a

= return -1 + 1

= 0

Since condition is false. But printf function will print 0. It is bug of c language.

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback