C language – Preprocessors and Header files in C

preprocessor directives in c language

In C language the C preprocessor is often known as a macro processor that is used automatically by the C compiler to transform your program before 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 intended to be used only with C, C++, and Objective C source code.

Preprocessor in c programming

Initial Processing

In C language the preprocessor performs a series of textual transformations on its input. These happen before all other processing.

Conceptually, they happen in a rigid order, and the entire file is run through each transformation before the next one begins.

C actually does them all at once, for performance reasons. these transformations correspond roughly to the first three “phases of translation”.

The Preprocessor in C language

The preprocessor contains any operations in the preprocessing language, it will be transformed first.

This stage corresponds roughly to the standard’s translation phase and is what most people think of as the preprocessor’s job.

The preprocessing language consists of directives to be executed and macros to be expanded. Its primary capabilities are:

  • 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. The preprocessor will replace the macros with their definitions throughout the program. Some macros are automatically defined for you.
  • Conditional compilation: 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 where each source line originally came from.
  • Diagnostics: You can detect problems at compile time and issue errors or warnings.

 

Header files in C language

A header file is a file containing C declarations and macro definitions to be shared between several source files.

You request the use of a header file in your program by including it, with the C preprocessing directive #include‘.

File inclusion

This is used to include an external file, which contains functions or some other macro definitions to our source program, so do not rewrite the functions and macros in our source program.

Syntax:
       #include"file name"
       #include 
Example
      #include 
      #include"loop.c"

here “stdio.h” is the file containing standard input and output function in ‘C‘ standard directory, “loop.c” is the program written by the user, it frequently occurs in many source programs.

Computed Includes

Sometimes it is necessary to select one of several different header files to be included in your program.

They might specify configuration parameters to be used on different sorts of operating systems, for instance. You could do this with a series of conditionals,

    #if SYSTEM_1
    # include "system_1.h"
    #elif SYSTEM_2
    #  include  "system_2.h"
    #elif  SYSTEM_3
       .....
    #endif

That rapidly becomes tedious, the preprocessor offers the ability to use a macro for the header name. This is called a computed include.

Instead of writing a header name as the direct argument of ‘#include’, you simply put a macro name there instead

#define SYSTEM_H "system_1.h"
        ....
#include SYSTEM_H

SYSTEM_H will be expanded, and the preprocessor will look for ‘system_1.h’ as if the #include had been written that way originally.

Macros in C language

In the C programming language, a macro is a fragment of code that has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros.

They differ mostly in what they look like when are used. Object-like macros resemble data objects when used, function like macros resemble function calls.

You may define any valid identifier as a macro, even if it is a C keyword. The preprocessor does not know anything about keywords.

Macro substitution

This is used to define symbolic constants in the source program. The identifier or string or integer defined is replaced by Macro substitution.

Syntax:
#define identifier string/integer

The preprocessor accomplishes the task specified in the ‘# define statement. This statement is also placed before the main() function in the source program.

1) Simple macros in c language

This is commonly used to define symbolic constants

Example:
#define  age 20
#define  CITY "CHENNAI"
#define  g   9.8
#define  A   10

The #define A 10 macro substitutes ‘A’ with 10 in all occurrences in the source program. The Expression can also be defined in macros as follows.

Example:
  #define A 10-5
  #define B 20-10

Consider an arithmetic operation in source program : c=(A) + (B). The results of the above expressions gives: c = (10-5) + (20-10). The literal text can also be substituted using macros.

Example:
#define exp if(10>5)
 #define True printf("Greater");
 #define false else printf("lesser");

To build the above macro as a statement like exp True False gives

if(10>5)
printf("Greater");
else
printf("Lesser");

2) Argumented macros

The argument macros are used to define more complex and useful forms of replacements in the source program.

Syntax:
#define identifier (v1 v2 v5...v4) string/integer

Example program

Program to print square and cube values by using macros substitutions.

Output:

Square value of 5 is 25
Cube value of 3 is 27

Explanation: In the above program initially the macro substitution is substituted for square and cube outside the main function. This demonstrates the macro substitutions.

The body of the program square and cube value is accessed and prints the values as input.

3) Nested macros

The macros are defined within another macro are called nested macros.

Example:
 #define A 5
 #define B A +2

Function-like Macros

You can also define macros whose use looks like a function call. These are called function-like macros.

To define a function-like macro, we use the same ‘#define’ directive, but you  put  a pair of parenthesis immediately after the macro name

Example:
#define lang_init() c_init()
lang_init()
c_init()

Preprocessor directives in C programming

preprocessor in c programming language

Rules for defining preprocessors in C language

  • Every preprocessor must start with a # symbol
  • The preprocessor is always placed before the main() function.
  • The preprocessor cannot have termination with semicolons.
  • There is no assignment operator #define statement
  • The conditional macro must be terminated (#ifdef, #endif).

Conditional inclusion in C language

#ifdef

The simplest sort of conditional  is

#ifdef MACRO
 controlled text
#endif

This block is called a conditional group. Controlled text will be included in the output of the preprocessor if and only if MACRO is defined. We say that the conditional succeeds if MACRO is defined, fails if it is not.

The controlled text inside of a conditional can include preprocessor directives. They are executed only if the conditional succeeds.

#if

The ‘#if’ directive allows you to test the value of an arithmetic expression, rather than the mere existence of one macro.

Syntax:
#if expression
   controlled text
#endif

Expression is a C expression of integer type, subject to stringent restrictions.

#define preprocessor in C language

The special operator-defined is used in #if and #elif expressions to test whether a certain name is defined as a macro.

defined is useful when you wish to test more than one macro for existence at once.

Example:
#if defined(_vax_)||defined (_ns16000_)

would succeed if either of the names _var_ or _ns16000_ is defined as a macro.

conditionals are written like this:

#if defined BUFSIZE && BUFSIZE >= 1024

    can generally be simplified to just #if BUFSIZE >= 1024, since if BUFSIZE is not defined, it will be interpreted as having the value zero.

#else macro in c language

The ‘#else’ directive can be added to a conditional to provide alternative text to be used if the condition fails. This is what it looks like

#if expression
 text-if-true
 #else  /* Not expression */
 text-if-false
 #endif /* not expression */

If an expression is nonzero, the text-if-true is included and the text-if-false is skipped. If an expression is zero, the opposite happens. You can use #else with #ifdef and #ifndef‘, too.

#elif

One common case of nested conditionals is used to check for more than two possible alternatives. For example, you might have

#if x == 1
...
#else   /* x != 1 */
  #if x == 2
  ...
   #else  /* x != 2 */
  ...
  #endif  /* x != 2 */
#endif   /* x != 1 */

Another conditional directive, #elif allows this to be abbreviated as follows

#if x == 1
...
  #elif x == 2
  ...
   #else  /* x != 2 and x != 1 */
   ...
#elseif  /* x != 2 and x != 1 */

#elif stands for else if’ it does not require a matching ‘endif’ of its own. Like #if‘, the #elif directive includes an expression to be tested.

The text following the ‘#elif’ is processed only if the original ‘#if‘ condition failed and the #elif condition succeeds.

More than one #elif can go in the same conditional group. Then the text after each #elif’ is processed only if the #elif’ condition succeeds after the original #if and all previous #elif directives within it have failed.

#else is allowed after any number of #elif’ directives, but #elif may not follow ‘#else‘.

Example Program

Program to print the values using macro substitution

Output:

The value is : 40

Explanation: This program displays the value using printf() statement which is defined in the preprocessor.

FAQ's