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

Discussion

Predict the output of following program?

#include <stdio.h>
#define MAX 1000
int main()
{
   int MAX = 100;
   printf("%d ", MAX);
   return 0;
}

 

  • A.1000
  • B.100
  • C.Compiler Error
  • D.Garbage Value

Answer: C

Correct answer: Compiler Error

The macro replaces the identifier MAX everywhere it appears, including in the declaration. The compiler therefore sees

int 1000 = 100;

which is a syntax error. The preprocessor knows nothing about scope or declarations - it is a text substitution, and this is why macro names are conventionally written in capitals, so they are not accidentally reused as variable names.

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

Post your comment

Please log in or register to post a comment.