Day#35 'C' Coding Challenge

Day#35 'C' Coding Challenge

What is the output of the below program?

#include <stdio.h> #define TALENT 100 void main() { #define TALENT 200 printf("%d", TALENT); }

------------------------------------------------------------

The above code tests two scenarios

1. preprocessor directive
2. Can we declare preprocessor directive anywhere in the code?
3. Which preprocessor has preference if there is a preprocessor outside and inside function?

--------------------------------------------------------------

Preprocessor directives are executed before the actual code compilation and can be thought as simple text distribution.
They start with '#' symbol.
The other preprocessor directives are.
  • #include
  • #define
  • #error
  • #pragma
  • #ifdef
  • #ifndef
  • #undef
  • #if
  • #ednif
  • #elif
  • #else
In our code #define is used and as per that Macro, 100 is substituted wherever TALENT is used.
However, there is a same macro inside the main().
So, this preprocessor has the priority and when TALENT is printed, it prints 200 and not 100

Post a Comment

0 Comments