Wednesday, June 15, 2011

 

Ifdef preprocessing

These provide a rapid way to "clip" out and insert code.

Consider;

     #define FIRST

main()
{
int a, b, c;

#ifdef FIRST
a=2; b=6; c=4;
#else
printf("Enter a:");
scanf("%d", &a);

printf("Enter a:");
scanf("%d", &a);

printf("Enter a:");
scanf("%d", &a);
#endif
additonal code

Note that if FIRST is defined (which it is in the above) the values of a, b and c are hardcoded to values of 2, 6 and 4. This can save a lot of time when developing software as it avoids tediously typing everything in each and everytime you run your routine. When FIRST is defined, all that is passed to the compiler is the code between the #ifdef and the #else. The code between the #else and the #endif is not seen by the compiler. It is as if it were all a comment.

source: http://www.phanderson.com/C/preprocess.html


Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?