C pre-processor

tutorial and example
tutorial and example

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler. All pre-processor commands begin with hash symbol (#).

The C pre-processor provides various facilities that are:

  • inclusion of header
  • Macro expansion
  • Conditional compilation
  • Line control
Preprocessor Syntax/Description
Macro Macro is used to define the constant value and data types.

Syntax: #define

Header file inclusion The source code of the file “file_name” is included in the main program at the specified place.

Syntax: #include <file_name>

Conditional compilation It is used to set of commands in source program.

Syntax: #ifdef, #endif, #if, #else, #ifndef

Other directives #undef is used to defined a macro variable and #Pragma is used to call a function before and after main function.

Syntax: #undef, #pragma

In C language, there are various stages of execution process. The given following diagram will help you:

There are following important pre-processor directories in C language.

Directive Description
#define Substitutes a pre-processor macro.
#include It is used to inserts a particular header from another file.
#undef It is used to undefine a preprocessor macro.
#ifdef It is used to return true if this macro is defined.
#ifdef It is used to return if this macro is defined.
#if It is used to check compile time condition.
#else The alternative for #if.
#elif #if and #else in one statement.
#endif It is used to end pre-processor conditional.
#error It is used to print error message on stderr.
#pragma Issues special command to the compiler, using a standardized method.

Example: 1

Example 2

C Macros

A macro is a name which stands for a fragment of code. Before using macro we must define it with ‘#define’ directive. It is the name of macro.

There are two types of macros.

  1. Object-like macros
  2. Function-like macros 
  1. Object Like Macros:

The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants.

Example: Pi =3.147

  1. Function like Macros:

Function like macros are used as function call. Let us consider an example.

Here MAX is the macro name.

There are some predefine macros :

Predefined Macros Description
_DATE_ It is defined as current date with “MMM DD YYY” format.
_TIME_ It is defined as current time with “HH:MM:SS” format.
_FILE_ It contains the current filename as a string literal.
_STDC_ It is defined as 1 when compiler compiles with the ANSI standard.
__STDC_VERSION__ It is defined as standard’s version
_GNUC_ It is defined if and only if this is  GNU C. etc

Example of Macros

C #define and #include.

C #defined:

It defines constant value. It can be any of the basic data types.

Syntax:

Example:1

Output

3.140000

Example:2

Output

value of number : 3.140000

value of letter : A

value of letter_sequence : C_Tutorial

value of backslash_char  : ?

#include:

It is used to paste code of given file into current file. We can use system-defined and used-defined header files.

There are two types of #include directive.

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

Example

C #undef:

It is used to check undefined the constant or macro. It is defined by #define.

Syntax:

Example1:

Output

Compile time Error: PI undeclared

Example2:

#ifdef, #else and #endif

The ‘#ifdef’ directive is used to check whether macro is defined or not. If it is defined then, ‘if’ clause statements are included in source code.

Otherwise “else” clause statements are included in source file for compilation and execution.

Example : #ifdef

Output

You are Eligible for voting

C #Pragma:

The C #Pragma is used to call a function before and after main function. It also provides additional information to the compiler.

There are several types of pragmas in GCC compiler.

  • AArch64 Pragmas
  • ARM Pragmas
  • M32C Pragmas
  • MeP Pragmas
  • RS/6000 and PowerPC Praglmas
  • S/390 Pragmas:
  • Darwin Pragmas:
  • Solaris Pragmas: etc.

Syntax:

Example:

Output

Now we are in main function