12.20 – Preprocessor operators and Misc. Directives in C

by subbu on January 2, 2014

Preprocessor operators in C language

#, ## are two preprocessor operators used with #define statements.

Stringize operator (#):

It keeps the argument in double quotations automatically. Converting a token into a string using # operator is called stringification.

#include<stdio.h>
#define name(s) #s
inr main()
{
 printf(name(Hello World));
 return 0;
}

Output:
Hello world

Example explained:

stringification in C

Here we send hello world as argument to macro name. The preprocessor expands the argument (hello world) in double quotations.

Pasting operator (##):

It concatenates the arguments

#include<stdio.h>
#define add(x,y) x##y
int main()
{
 int ab=10;
 printf("%d",add(a,b));
 return 0;
}

Output:
10

Example explained:

pasting in c

We sent a, b as arguments to the macro add(x,y) which are concatenated by the preprocessor as ab

Predefined macro names in C

C language defines some of the Macros like
_ _DATE_ _
_ _TIME_ _
_ _LINE_ _
_ _FILE_ _
_ _STDC_ _

_ _DATE_ _ has the date of translation from source code to object code and _ _TIME_ _ has the time of translation from source code to object code.
Say for example, a C program is translated into object code on 16/10/2013 at 3:45 A.M  then _ _DATE_ _ would be Oct 16 2013 and _ _TIME_ _ would be 03:45:25
If we want to know the date and time of compilation of a program then we can write as

printf("\n%s",__DATE__);
printf("\n%s",__TIME__);

_ _LINE_ _ has the line number of currently compiled line of code as positive integer. Say for example, If we are printing the value of _ _LINE_ _ in the 12th line of program then it would be 12. If want to know the current line number then we can write as

printf("\n%d",__LINE__);

_ _FILE_ _ has the name of source file which is being compiled. If we want to know the name of source file from which the object code was generated then we can write as

printf("\n%s",__FILE__);

_ _STDC_ _ is a macro defined only when the compiler is a standard C extension. Say for example Turbo C has non standard C extensions. Hence macro _ _STDC_ _ is not defined in Turbo C but, defined in MinGW because MinGW implements standard C
To know whether a compiler is a standard C complaint or not, we can write as

#ifdef __STDC__
 printf("\nYou are using standard C implementation");
#else
 printf("\nYou are using non standard C extension");
#endif

Putting all together:

#include<stdio.h>
int main()
{
printf("\n%s",__DATE__);
printf("\n%s",__TIME__);
printf("\n%s",__FILE__);
printf("\n%d",__LINE__);
#ifdef __STDC__
 printf("\nYou are using standard C implementation");
#else
 printf("\nYou are using non standard C extension");
#endif
printf("\n");
return 0;
}

Output with MinGW gcc:
Feb 17 2014
05:31:23
DEMO.C
7
You are using standard C implementation

Output in Turbo C:
Feb 17 2014
05:38:40
DEMO.C
7
You are using non standard C extension

#line preprocessor directive:

We know that, _ _LINE_ _ is a Macro that contains the line number of the currently compiling line of code and _ _FILE_ _ is a Macro that contains the name of source file from which object code is being generated.
It is generally used for debugging and error handling

#include<stdio.h>
int main()
{
 printf("Line number %d",__LINE__);
 printf("\nFile name %s",__FILE__);
 return 0;
}

Output:
Line number 4
File name DEMO.C

In the above example _ _LINE_ _ is 4 because we are using it in the 4th line of source code and _ _FILE_ _ is DEMO.C because we are compiling a program saved as demo.c
Now by using #line preprocessor directive we can set new values to _ _LINE_ _ and _ _FILE_ _

#line syntax

#include<stdio.h>
#line 200 "fox"         /* sets line number to 200 and file name to "fox" */
int main()              /* resets the line counter */
{                                  /* line number 201 */
printf("Line number %d",__LINE__); /* line number 202 */
printf("\nFile name %s",__FILE__);
return 0;
}

Output:
Line number 202
File name fox

In the above example, line counter begins with 200. The printf() statement prints the line number 202 because it is the 3rd line after #line
Though the name of above program is “demo.c” printed the file name as “fox” as defined with #line

#error preprocessor directive

The preprocessor directive #error reports a fatal error by terminating the compilation. The text following the #error directive would be displayed as an error message

#include<stdio.h>
#ifndef __STDC__
#error Not using standard C implementation
#endif
int main()
{
 printf("Hello World");
 return 0;
}

Output with MinGW
Hello World

Output in Turbo C
Fatal demo.3: Not using standard C implementation

Because Turbo C has no macro __STDC__, throws fatal error by terminating the compilation

#warning preprocessor directive

Some of the C implementations support #warning directive. It works similar to #error but, compiles the program by just showing the warning message

Previous post:

Next post: