Wednesday, March 21, 2012

preprocessor


1) What will be output of following code?

#define max 10
void main()
{
    int i;
    i=++max;
    clrscr();
    printf("%d",i);
    getch();
}

output: compiler error.
Explanation:  max is preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant 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 as:


void main()
{
     int i;
     i=++10;
     clrscr();
     printf("%d",i);
     getch();
}

To know how we can see intermediate file click here

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) What will be output of following code?

#define max 10+2
void main()
{
    int i;
    i=max*max;
    clrscr();
    printf("%d",i);
    getch();
}


Output:32

Explanation: 

max is preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to the its constant value without any calculation 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:


void main()
{
    int i;
    i=10+2*10+2;
    clrscr();
    printf("%d",i);
    getch();
}

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


(3) What will be output of following code?

#define A 4-2
#define B 3-1
void main()
{
     int ratio=A/B;
     printf("%d ",ratio);
     getch();
}


Output:3
Explanation: A and B preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value without any calculation 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:


void main()
{
    int ratio=4-2/3-1;
    printf("%d ",ratio);
    getch();
}


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


(4) What will be output of following code?

#define MAN(x,y) (x)>(y)?(x):(y)
void main()
{
       int i=10,j=9,k=0;
       k=MAN(i++,++j);
       printf("%d %d %d",i,j,k);
       getch();
}


Output: 11 11 11

Explanation: Preprocessor’s macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value without any calculation in entire the program before the compilation. Thus program will be converted as:


void main()
{
    int i=10,j=9,k=0;
    k=(i++)>(++j)?(i++):(++j);
    printf("%d %d %d",i,j,k);
    getch();
}

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) What will be output of following code?

#define START main() {
#define PRINT printf("*******");
#define END }
START
PRINT
END


Output: *******
Explanation: 

Preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value i.e. symbols without any calculation in entire the program before the compilation. Thus program will be converted as:


main()
{
    printf("*******");
}


(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");
void main()
{
      int volume =CUBE(3+2);
      clrscr();
      printf("%d %d ",volume,N);
      PRINT
      getch();
}


Output: 17 6
Explanation:

Preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value without any calculation in entire the program before the compilation. Thus program will be converted as:

void main()
{
    int volume =(3+2*3+2*3+2);
    clrscr();
    printf("%d %d ",volume,5+1);
    PRINT
    getch();
}




2 comments:

  1. trex titanium headphones - Titanium Arts
    T.I. T.I. T.I.T. I. where is titanium found T.I. T.I. T.I. T.I. T.I. ford edge titanium 2019 T.I. T.I. T.I. T.I. T.I. T.I. titanium nose stud T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. T.I. titanium sheet T.I. T.I. T.I. tube supplier T.I. T.I. T.I.

    ReplyDelete