Friday, March 18, 2011

Program for Preprocessor with Explanation

Program
# include

#define VAL 35 // A
#define HELLO "HELLO"; // B

main ()
{
int res;

res = VAL-5; // C
printf ("res = VAL-5: res == %d\n", res);
printf ( HELLO); //D
}

Statements A and B indicate preprocessor directives. VAL is defined as integer 35 and HELLO is a string as "HELLO". Whenever VAL and HELLO appear, they are replaced by the specified values.

In statement C, VAL 35 is replaced by VAL −5. So the statement becomes

res = 35-5;

Statement D, after replacement, becomes

printf ("HELLO")

The preprocessor directives are not C statements, so they do not end with semicolons.

The include directive tells the compiler to include all the contents of a specified file in the source file before giving the source file for compiling.

Explanation
The preprocessor substitutes strings that are specified by using define directive

#define constant identifer "value"

Following are valid define expressions:

#define TRUE 1
#define FALSE 0
#define BS '\b'
#define TAB '\011'

No comments:

Post a Comment