C Preprocessor Directives

  • Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing.
  • Commands used in preprocessor are called preprocessor directives and they begin with “#” symbol.
  • Below is the list of preprocessor directives that C language offers.
S.no
Preprocessor
Syntax
Description
1
Macro
#define
This macro defines constant value and can be any of the basic data types.
2
Header file inclusion
#include
The source code of the file “file_name” is included in the main program at the specified place
3
Conditional compilation
#ifdef, #endif, #if, #else,  #ifndef
Set of commands are included or excluded in source program before compilation with respect to the condition
4
Other directives
#undef, #pragma
#undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program

A program in C language involves into different processes.  Below diagram will help you to understand all the processes that a C program comes across.

Example program for #define, #include preprocessors in C:
    • #define  -   This macro defines constant value and can be any of the basic data types.
    • #include   -   The source code of the file “file_name” is included in the main C program where “#include ” is  mentioned.
#include

#define height 100
#define number 3.14
#define letter 'A'
#define letter_sequence "ABC"
#define backslash_char '\?'

void main()
{
   printf("value of height    : %d \n", height );
   printf("value of number : %f \n", number );
   printf("value of letter : %c \n", letter );
   printf("value of letter_sequence : %s \n", letter_sequence);
   printf("value of backslash_char  : %c \n", backslash_char);

}
Output:

value of height : 100
value of number : 3.140000
value of letter : A
value of letter_sequence : ABC
value of backslash_char : ?

Related

C Language 4363978550772672715

Post a Comment

emo-but-icon

item