Preprocessing Directives in C With Example

aimtocode

Preprocessor Definition

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation.

It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation.

The C preprocessor provides four separate facilities that you can use as you see fit:

  • Inclusion of header files. These are files of declarations that can be substituted into your program.
  • Macro expansion. You can define macros, which are abbreviations for arbitrary fragments of C code, and then the C preprocessor will replace the macros with their definitions throughout the program.
  • Conditional compilation. Using special preprocessing directives, you can include or exclude parts of the program according to various conditions.
  • Line control. If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came from.

Preprocessing Directives

Most preprocessor features are active only if you use preprocessing directives to request their use.

Preprocessing directives are lines in your program that start with `#'.

The `#' is followed by an identifier that is the directive name. For example, `#define' is the directive that defines a macro. Whitespace is also allowed before and after the `#'.

Sr.No. Directive & Description
1

#define

#define is used to create symbolic constants (known as macros) in C programming language. This preprocessor command can also be used with parameterized macros.

2

#include

#include is used to insert specific header file into C program.

3

#undef

#undef is used to destroy a macro that was already created using #define.

4

#ifdef

#ifdef returns TRUE if the macro is defined and returns FALSE if the macro is not defined.

5

#ifndef

#ifndef returns TRUE if the specified macro is not defined otherwise returns FALSE.

6

#if

#if uses the value of specified macro for conditional compilation.

7

#else

#else is an alternative for #if.

8

#elif

#elif is a #else followed by #if in one statement.

9

#endif

#endif is used terminate preprocessor conditional macro.

10

#error

#error is used to print error message on stderr.

11

#pragma

#pragma is used to issue a special command to the compiler.


Preprocessors Examples

Analyze the following examples to understand various directives.

  #define MAX_ARRAY_LENGTH 20

This directive tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #define for constants to increase readability.

  #include <stdio.h>
  #include "myheader.h"

These directives tell the CPP to get stdio.h from System Libraries and add the text to the current source file. The next line tells CPP to get myheader.h from the local directory and add the content to the current source file.

  #undef  FILE_SIZE
  #define FILE_SIZE 42

It tells the CPP to undefine existing FILE_SIZE and define it as 42.

  #ifndef MESSAGE
    #define MESSAGE "You wish!"
  #endif

It tells the CPP to define MESSAGE only if MESSAGE isn't already defined.

  #ifdef DEBUG
    /* Your debugging statements here */
  #endif

It tells the CPP to process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to the gcc compiler at the time of compilation.



Output:

 Enter Diameter of a circle : 40
 Radius of diameter is :  20.000000

Predefined Macros

In C programming language, there are some pre-defined macros and they are as follows...

Directive Description
__FILE__ This macro expands to the name of the current input file, in the form of a C string constant. The precise name returned is the one that was specified in `#include' or as the input file name argument.
__LINE__ This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it's a pretty strange macro, since its "definition" changes with each new line of source code.
__DATE__ This macro expands to a string constant that describes the date on which the preprocessor is being run. The string constant contains eleven characters and looks like `"Feb 1 1996"'.
__TIME__ This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters and looks like `"23:59:01"'.
__STDC__ This macro expands to the constant 1, to signify that this is ANSI Standard C. (Whether that is actually true depends on what C compiler will operate on the output from the preprocessor.)
__STDC_VERSION__ This macro expands to the C Standard's version number, a long integer constant of the form `yyyymmL' where yyyy and mm are the year and month of the Standard version.
__GNUC__ This macro is defined only when the entire GNU C compiler is in useed
__GNUC_MINOR__ The macro contains the minor version number of the compiler. This can be used to work around differences between different releases of the compiler.
__GNUG__ The GNU C compiler defines this when the compilation language is C++; use `__GNUG__' to distinguish between GNU C and GNU C++.
__cplusplus__ You can use `__cplusplus' to test whether a header is compiled by a C compiler or a C++ compiler.
__STRICT_ANSI__ This macro exists primarily to direct certain GNU header files not to define certain traditional Unix constructs which are incompatible with ANSI C.
__BASE_FILE__ This macro expands to the name of the main input file, in the form of a C string constant.
__INCLUDE_LEVEL__ This macro expands to a decimal integer constant that represents the depth of nesting in include files. The value of this macro is incremented on every `#include' directive and decremented at every end of file.
__VERSION__ This macro expands to a string constant which describes the version number of GNU C. The string is normally a sequence of decimal numbers separated by periods, such as `"2.6.0"'.
__OPTIMIZE__ GNU CC defines this macro in optimizing compilations. It causes certain GNU header files to define alternative macro definitions for some system library functions.
__CHAR_UNSIGNED__ GNU C defines this macro if and only if the data type char is unsigned on the target machine. It exists to cause the standard header file `limits.h' to work correctly.
__REGISTER_PREFIX__ This macro expands to a string (not a string constant) describing the prefix applied to CPU registers in assembler code.
__USER_LABEL_PREFIX__ Similar to __REGISTER_PREFIX__, but describes the prefix applied to user generated labels in assembler code.

Example:



Output:

This is in func
This is in main
This is in func  				
aimtocode