Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

#include <stdio.h>
#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;
}

 

  • A.Result = -100.000000
  • B.Result = -25.000000
  • C.Result = 0.000000
  • D.Result = 100.000000

Answer: A

Correct answer: Result = -100.000000

The unparenthesised macro turns the expression into

a = 2*(s-u*t)/t*t;

and with / and * at equal precedence, left to right:

\[ 2 \times (10 - 60) = -100, \quad -100 / 2 = -50, \quad -50 \times 2 = -100 \]

So -100.000000, not the intended \(-100/4 = -25\).

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.