Showing posts with label preprocessor. Show all posts
Showing posts with label preprocessor. Show all posts

Sunday, December 6, 2009

#ifdef & #endif

#ifdef something
int some=0;
#endif
main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer:
Compiler error : undefined symbol some
Explanation:
This is a very simple example for conditional compilation. The
name something is not already known to the compiler making the
declaration
int some = 0;
effectively removed from the source code.





 #if something == 0
int some=0;
#endif
main()
{
int thing = 0;
printf("%d %d\n", some ,thing);
}
Answer
0 0
Explanation
This code is to show that preprocessor expressions are not the
same as the ordinary expressions. If a name is not known the
preprocessor treats it to be equal to zero.

Thursday, December 3, 2009

Using ## and # with define.

Stringize(#)

#define   message_for(a, b)  \
printf(#a " and " #b)
int main()
{
message_for(Carole, Debra);
}

Token pasting (##)
#define f(g,g2) g##g2
main()
{
int var12=100;
printf("%d",f(var,12));
}
Answer:
100

The preprocessor directives can be redefined anywhere in the program

#include
#define a 10
main()
{
#define a 50
printf("%d",a);
}
Answer:
50
Explanation:
The preprocessor directives can be redefined anywhere in the program. So
the most recently assigned value will be taken.

Wednesday, December 2, 2009

Some macros

#define SQR(x) ((x)*(x))

#define MAX(a,b) ( (a) > (b) ? (a) : (b)  )

#define ISLP(y)  (  (y % 400 == 0)  || (y %100 != 0 && y%4 == 0)  )

 #define ISLOWER(a)  (a>=97 && a<=127)

#define TOLOWER(a)  (a - 32)

Using define without assigning value

#include
#define NO
#define YES

int main()
{
    int i = 5,j;
    if (i>5)   j=YES;
    else j=NO;

    printf("%d",j);
}

Output: Expression syntax in function main
Explanation:  Because when assigning j with YES or NO we don't know what is the value of YES or NO.