Tuesday, June 12, 2012

Question of C Preprocessor with Detailed Solution

Question of c preprocessor with detailed solution

C programming preprocessor questions and explanations
(1) What will be output of following code?

#include
#define max 10

int main(){

    int i;

    i=++max;

    printf("%d",i);

    return 0;
}

(2) What will be output of following code?

#include
#define max 10+2

int main(){

    int i;

    i=max*max;

    printf("%d",i);

    return 0;
}

(3) What will be output of following code?

#include
#define A 4-2

#define B 3-1

int main(){

     int ratio=A/B;

     printf("%d ",ratio);

     return 0;
}

(4) What will be output of following code?

#include
#define MAN(x,y) (x)>(y)?(x):(y)

int main(){

       int i=10,j=9,k=0;

       k=MAN(i++,++j);

       printf("%d %d %d",i,j,k);

       return 0;
}

(5) What will be output of following code?

#include
#define START main() {

#define PRINT printf("*******");

#define END }

START

PRINT

END

(6) What will be output of following code?

#define CUBE(x) (x*x*x)

#define M 5

#define N M+1

#define PRINT printf("RITESH");

int main(){

      int volume =CUBE(3+2);

      printf("%d %d ",volume,N);

      PRINT

      return 0;
}

Solution section 


(1)
output: compiler error.
Explanation:


Here max is preprocessor macro symbol which process first before the actual compilation. First preprocessor replace the symbol to its value in entire the program before the compilation. So in this program max will be replaced by 10 before compilation. Thus program will be converted like this:

int main(){

     int i;

     i=++10;

     printf("%d",i);

     return 0;
}


In this program we are trying to increment a constant symbol.


Meaning of ++10 is:
10=10+1
or 10=11


Which is error because we cannot assign constant value to another constant value .Hence compiler will give error.

(2)


Output: 32
Explanation:


Here max is preprocessor macro symbol which process first before the actual compilation start. Preprocessor replace the symbol to its value in entire the program before the compilation. So in this program max will be replaced by 10+2 before compilation. Thus program will be converted as:
 
int main(){

    int i;

    i=10+2*10+2;

    printf("%d",i);

    return 0;
}


now i=10+2*10+2
i=10+20+2
i=32

(3)


Output: 3
Explanation:

A and B are preprocessor macro symbol which process first before the actual compilation start. Preprocessor replace the symbol to its value in entire the program before the compilation. So in this program A and B will be replaced by 4-2 and 3-1 respectively before compilation. Thus program will be converted as: 

int main(){

    int ratio=4-2/3-1;

    printf("%d ",ratio);

    return 0;
}


Here ratio=4-2/3-1
ratio=4-0-1
ratio=3

(4)


Output: 11 11 11

Explanation:


Preprocessor’s macro which process first before the actual compilation. Thus program will be converted as: 

int main(){

    int i=10,j=9,k=0;

    k=(i++)>(++j)?(i++):(++j);

    printf("%d %d %d",i,j,k);

    return 0;
}


now k=(i++)>(++j)?(i++):(++j);
first it will check the condition
(i++)>(++j)


i++ i.e. when postfix is used with variable in expression then expression is evaluated first with original value then variable is incremented

Or 10>10


This condition is false.
Now i = 10+1 = 11
There is rule, only false part will execute after? i.e. ++j, i++ will be not execute.
So after ++j
j=10+1=11;


And k will assign value of j .so k=11; 

(5)


Output: *******
Explanation:

This program will be converted as: 

main(){

    printf("*******");

}

(6)


Output: 17 6
Explanation: This program will be converted as:

int main(){

    int volume =(3+2*3+2*3+2);

    printf("%d %d ",volume,5+1);

    PRINT

    return 0;
}

If you have any quires or suggestions in above question of c preprocessor with  detailed solution, you can ask here

No comments:

Post a Comment

Thank for visting this blog . Please Pass on this information to all of Our friends to utilize this oppurtunity.