Showing posts with label define. Show all posts
Showing posts with label define. Show all posts

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.