Tuesday, January 17, 2012

C Preprocessor Questions

Question 1:
#include<stdio.h>
#define f(g,g2) g##g2
int main()
{
   int var12 = 100;
   printf("%d", f(var,12));
   getchar();
   return 0;
}
Output: 100
The operator ## is called “Token-Pasting” or “Merge” Operator or concatenation c preprocessor operator.It merges two tokens into one token. So, after preprocessing, the main function prints 100.

Question 2:
#define FALSE -1
#define TRUE   1
#define NULL   0
main () {
   if(NULL)
puts("NULL");
   else if(FALSE)
puts("TRUE");
   else
puts("FALSE");
   }
Answer:
TRUE
Explanation:Preprocessor doesn't replace the values given inside the double quotes.
The check by if condition is boolean value false so it goes to else. In
second if -1 is boolean value true hence "TRUE" is printed.

No comments:

Post a Comment