Home / C Programming / C Preprocessor :: Discussion

Discussion :: C Preprocessor

  1. What will be the output of the program?

     #include
     #define SQUARE(x) x*x 
    
     int main() 
     {  
       float s=10, u=30, t=2, a;  
       a = 2*(s-u*t)/SQUARE(t);     
       printf("Result = %f", a);     
       return 0;
     } 
    

  2. A.

    Result = -100.000000

    B.

    Result = -25.000000

    C.

    Result = 0.000000

    D.

    Result = 100.000000

    View Answer

    Workspace

    Answer : Option A

    Explanation :

    The macro function SQUARE(x) x*x calculate the square of the given number 'x'. (Eg: 102)

    Step 1: float s=10, u=30, t=2, a; Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.

    Step 2: a = 2*(s-u*t)/SQUARE(t); becomes,

    => a = 2 * (10 - 30 * 2) / t * t; Here SQUARE(t) is replaced by macro to t*t .

    => a = 2 * (10 - 30 * 2) / 2 * 2;

    => a = 2 * (10 - 60) / 2 * 2;

    => a = 2 * (-50) / 2 * 2 ;

    => a = 2 * (-25) * 2 ;

    => a = (-50) * 2 ;

    => a = -100;

    Step 3: printf("Result=%f", a); It prints the value of variable 'a'.

    Hence the output of the program is -100


Be The First To Comment