0% found this document useful (0 votes)
307 views

C Preprocessor

The document discusses C preprocessor directives. It explains that the preprocessor processes the source code before it is passed to the compiler. The main preprocessor directives are macros, file inclusion, and conditional compilation. Macros allow text to be defined and replaced. File inclusion inserts the contents of another file. Conditional compilation includes or excludes blocks of code based on conditions. Other directives include #undef to undefine macros and #pragma for special features like specifying startup and exit functions.

Uploaded by

RAHUL ARORA
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
307 views

C Preprocessor

The document discusses C preprocessor directives. It explains that the preprocessor processes the source code before it is passed to the compiler. The main preprocessor directives are macros, file inclusion, and conditional compilation. Macros allow text to be defined and replaced. File inclusion inserts the contents of another file. Conditional compilation includes or excludes blocks of code based on conditions. Other directives include #undef to undefine macros and #pragma for special features like specifying startup and exit functions.

Uploaded by

RAHUL ARORA
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C Preprocessor Directives

Ritu Devi
C Preprocessor statements.
• C preprocessor is a program that processes our source
program before it is passed to the compiler
• Features preprocessor directives: The preprocessor offers
several features called preprocessor directives
– Each preprocessor directive starts with a # symbol.
– There can be only one directive on a line.
– There is no semicolon at the end of a directive.
– To continue a directive on next line, we should place a backslash at the
end of the line.
– The preprocessor directives can be placed anywhere in a program
(inside or outside functions) but they are usually written at the
beginning of a program. '
– A directive is active from the point of its appearance till the end of the
program.
• Our program passes through several processors before it is
ready to be executed.
• The main functions performed by the preprocessor directives
are·
1. Macros.
2. File Inclusion.
3. Conditional Compilation.
4. Other directives
Macro Expansion: #define
• The general syntax is-
#define macro_name macro_expansion
• Here macro_name is any valid C identifier, and it is generally
taken in capital letters to distinguish it from other variables.
• The macro_expansion can be any text.
• For example-
• #define PI 3.14159265
• #define MAX 100
• The C preprocessor searches for macro_name in the C source
code and replaces it with the macro_expansion.
• Some more examples:
Macros with Arguments
• The general syntax is
• #define macro_name(arg 1, arg2, ......) macro_expansion
• For example
• some more examples:

• #undef Directive :
The definition of a macro will exist from the #define directive
tiII the end of the program. If we want to undefine this macro
we can use the #undef directive.
– Syntax: #undef macro_name
Problems with Macros
File Inclusion
• This type of preprocessor directive tells the compiler to
include a file in the source code program. 
• The filename should be within angular brackets or double
quotes. The syntax is-
#include "filename” /* user defined file*/
#include <filename> /* header file or standard file */

• The preprocessor replaces the #include directive by the


contents of the specified file
Conditional Compilation
• Conditional Compilation directives are type of directives
which helps to compile a specific portion of the program or to
skip compilation of some specific part of the program based
on some conditions. 
• This can be done with the help of two preprocessing
commands ‘ifdef‘ and ‘endif‘.

• If the macro with name as ‘macroname‘ is defined then the


block of statements will execute normally but if it is not
defined, the compiler will simply skip this block of statements.
Other directives:
• #undef Directive: 
• #pragma Directive: This directive is a special purpose
directive and is used to turn on or off some features.
– #pragma startup and #pragma exit: These directives helps us to
specify the functions that are needed to run before program
startup( before the control passes to main()) and just before program
exit (just before the control returns from main()).
#include <stdio.h>

void func1();
void func2();  
#pragma startup func1
#pragma exit func2
  
void func1()
{     printf("Inside func1()\n");
}
void func2()
{
    printf("Inside func2()\n");
}
  
int main()
{
    void func1();
    void func2();
    printf("Inside main()\n");
      return 0;
}

You might also like