C PREPROCESSOR AND ITS TYPES

In the previous article we’ve studied about storage classes in c. In this article we’ll study about C PREPROCESSOR AND ITS TYPES. The C preprocessor is exactly what its name implies. It is a program that processes our source program before it is pass to compiler. The preprocessor offer several features. These called preprocessor directives. Each of the them starts with # symbol. These can be place anywhere in program. But most often placed at beginning of a program . further continuing our topic on C PREPROCESSOR AND ITS TYPES:

TYPES OF C PREPROCCESORS

Further we’ll discuss following types of preprocessor directives :

  • Macro expansion
  • File inclusion
  • Conditional compilation
  • Miscellaneous directives
MACRO EXPANSION

Take a look of the following program.

C PREPROCESSOR AND ITS TYPES
example program of macro expansion

In this program . Instead of writing 25 in the for loop . We are writing it in form of UPPER. Which we already defined before main(). #define UPPER 25. This is call as macro ‘definition’. Here UPPER is call as ‘macro template’. Whereas 25 is call as their corresponding ‘macro expansion’. When we compile the program ,before the source code passes to compiler. It is examined by the c preprocessor. For any macro definition. When it sees the #define directive. It goes through entire problem . And search for macro template. Wherever it finds one. It replaces the macro template with appropriate macro expansion.

A define directory is many a time use to define operators. It also be use even to replace a condition in C. For example AND, OR conditions. It can also be use to replace entire c statement. For example print f statements.

MACROS WITH ARGUEMENT

The macros we have use so far are call as simple macros. Macros can have arguments just has functions. But we have to kept some points in mind while using macros as arguments. There should be no blanks between macros template and its arguments. While we define argument. For example there should be no blank between AREA and (x) in definition. Also the entire macro expansion should be enclose within parenthesis. Next, Macros can be split into multiple lines by using ‘\’ (backslash). The backslash should be present at end of each line.