100% found this document useful (1 vote)
29 views

C Preprocessor Directives

C preprocessor

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
29 views

C Preprocessor Directives

C preprocessor

Uploaded by

lakshyagpta8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

C Preprocessor Directives

The C preprocessor modifies a source file before handing it over to the compiler,
allowing conditional compilation with #ifdef, defining constants with #define,
including header files with #include, and using builtin macros such as __FILE__. This
page lists the preprocessor directives, or commands to the preprocessor, that are
available:

• #include
• #define
• #undef
• #if
• #ifdef
• #ifndef
• #error
• __FILE__
• __LINE__
• __DATE__
• __TIME__
• __TIMESTAMP__
• pragma
• # macro operator
• ## macro operator

#include
#include <header name>
The include directive instructs the pre-processor to paste the text of the given file
into the current file. Generally, it is necessary to tell the preprocessor where to look
for header files if they are not placed in the current directory or a standard system
directory. This can be done either at compile time or as part of your compiler's
project file. This feature is implementation-specific, so see the compilers page for
more information.

If an included file cannot be found, compilation will cease with an error.

By the use of #include directive, we provide information to the preprocessor where


to look for the header files. There are two variants to use #include directive.

1. #include <filename>
2. #include "filename"

The #include <filename> tells the compiler to look for the directory where system
header files are held. In UNIX, it is \usr\include directory.

The #include "filename" tells the compiler to look in the current directory from
where program is running.

#define
The #define directive takes two forms: defining a constant and creating a macro.

The #define preprocessor directive is used to define constant or micro substitution

Defining a constant
#define token [value]

Example:
#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
}

Defining a parameterized macro


#define token(<arg> [, <arg>s ... ]) statement
For instance, here is a standard way of writing a macro to return the max of two
values.
#define MAX(a, b) ((a) > (b) ? (a) : (b))
Note that when writing macros there are a lot of small gotchas; you can read more
about it here: the c preprocessor . To define a multiline macro, each line before the
last should end with a \, which will result in a line continuation.

#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main() {
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
}

#undef
The #undef directive undefines a constant or preprocessor macro defined previously
using #define.

#undef token
For example:

#define E 2.71828
int e_squared = E * E;
#undef E

Usually, #undef is used to scope a preprocessor constant into a very limited region--
this is done to avoid leaking the constant (for example, changing a single letter like
E would be dangerous across a large program, but in a short scope, it is
comparatively safe. #undef is the only way to create this scope since the
preprocessor does not understand block scopes defined with { and }.

#include <stdio.h>
#define PI 3.14
#undef PI
main() {
printf("%f",PI);
}

#if
#if <value>
/* code to execute if this value is true */
#elsif lt;value>
/* code to execute if this value is true */
#else
/* code to execut otherwise
#endif
#if checks whether the value is true (in the C and C++ sense of everything but 0)
and if so, includes the code until the closing #endif. If not, that code is removed
from the copy of the file given to the compiler prior to compilation (but it has no
effect on the original source code file). There may be nested #if statements. It is
common to comment out a block of code using the following construction because
you cannot nest multi-line comments in C or C++.

#if 0
/* code */
#endif

#include <stdio.h>
#define NUMBER 1
void main() {
#if (NUMBER==0)
printf("1 Value of Number is: %d",NUMBER);
#endif

#if (NUMBER==1)
printf("2 Value of Number is: %d",NUMBER);
#endif
}

#ifdef
#ifdef <token>
/* code */
#else
/* code to include if the token is not defined */
#endif
ifdef checks whether the given token has been #defined earlier in the file or in an
included file. If so, it includes everything between it and the closing #else or, if no
#else is present, the closing #endif. #ifdef can be used with built-in token identifiers
set by the compiler to indicate that additional functionality is available. For instance,
the __cplusplus macro is defined in C++ but not C; you can use this fact to mix C
and C++ code using an #ifdef statement:
#ifdef __cplusplus
// C++ code
#else
// C code
#endif

#include <stdio.h>
#define NOINPUT
void main() {
int a=0; #ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
}

#ifndef
#ifndef <token>
/* code */
#else
/* code to include if the token is defined */
#endif
#ifndef checks whether the given token has been #defined earlier in the file or in an
included file; if not, it includes the code between it and the closing #else or, if no
#else is present, #endif statement. #ifndef is often used to make header
files idempotent by defining a token once the file has been included and checking
that the token was not set at the top of that file.

#ifndef _INCL_GUARD
#define _INCL_GUARD
#endif

#error
#error "This code should not compile"
The #error macro allows you to make compilation fail and issue a statement that will
appear in the list of compilation errors. It is most useful when combined
with #if/#elif/#else to fail compilation if some condition is not true. For example:
#ifndef __unix__ // __unix__ is typically defined when targetting Unix
#error "Only Unix is supported"
#endif

Four Major Types of Preprocessor Directives

1. Macro Expansion

In Macro expansion, we can specify two types of Macros with arguments:

We can also pass arguments to macros; it can be described with arguments that
perform similarly as functions.

Syntax:

#define name substitute text

Where,

• name: Here, we can define the micro template.

• replacement text : we can define it as the macro expansion.

• To write a macro name, we need to use capital letters.

• For better readability, we can define suitable names on certain macros.

• To modify program: We can change only the macro and it can reflect on the program.
Hence we do not need to change it every time.

Example: Basic Macro

#define PrintLOWER 50

void main()

int j;

for (j=1;i<=PrintLOWER; j++)

{
printf("\n%d", j);

Example: Macros With Certain Arguments

#define AREA(a) (5.18 * a * a)

void main()

float r = 3.5, x;

x = AREA (r);

printf ("\n Area of circle = %f", x);

Types of Predefined Macros

• ___TIME___ defines the current time using a character literal in “HH:MM: SS” format.

• ___STDC___ specifies as 1 when the compiler complies with the ANSI standard.

• ___TIMESTAMP___ specifies the timestamp “DDD MM YYYY Date HH:MM: SS”. It is


used to define the date and time of the latest modification of the present source file.

• ___LINE___ consists of a present line number with a decimal constant.

• ___FILE___ includes the current filename using a string literal.

• ___DATE___ shows the present date with a character literal in the “MMM DD YYYY”
format.

Example:

#include<stdio.h>

int main(){

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

printf("Date :%s\n", __DATE__ );

printf("Time :%s\n", __TIME__ );


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

printf("STDC :%d\n", __STDC__ );

return 0;

You might also like