As the name implies, it is a program that processes our source program before compilation. The C preprocessor, often known as a macro processor that is used automatically by the C compiler to transform your program before compilation.
THE 'C' PREPROCESSOR
1. Introduction
2. The preprocessing language
3. Header Files
4. Macros
5. Rules for defining preprocessor
As
the name implies, it is a program that processes our source program before
compilation. The C preprocessor, 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.

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.
• 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.
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
Header files serve the below mentioned purposes.
•
System header files declare the interfaces to parts of the operating system.
•
Your own header files contain declarations for interfaces between the source
files of your program.
Including
a header file produces the same results as copying the header file into each
source file that needs it. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place.
This
is used to include an external file, which contains functions or some other
macro definitions to our source program.
Syntax:
#include "file
name"
#include
<filename>
Where
'filename' is the name of the file,
that can be included in our source program.
When 'filename'
is quoted, it searches for that file in current directory and then in standard
directories. At this stage, the entire contents of the files are included in
the source program.
When
the file is included in Angle brackets (<>) the included file is searched
only in standard directories.
Example:
#include
<stdio.h>
#include
"loop.c"
Where
"stdio.h" is the file,
that contains 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.

A
macro is a fragment of code which has been given a name. Whenever the name is
used, it is replaced by the contents of the macro.
They
differ mostly in what they look like when they 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.
This
can be useful if you wish to hide a keyword such as const from an older
compiler that does not understand it.
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 '#define' statement. This statement is also placed before the main() function in source program. The
preprocessor replaces the every occurrence of an identifier by the specified
string or integer in source program.
i) Simple macros
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");
ii) Argumented macros
The
argumented macros are used to define more complex and useful form of
replacements in the source program.
Syntax:
#define
identifier (v1 v2 v5... v4) string/integer
Example: Program to
print square and cube values by using macro substitutions.
/* Program to demonstrate on macro substitutions */
#define sq(n) (n*n) /* macro substitutions */
#define cube(n) (n*n*n) /* macro substitutions */
#include <stdio.h>
main()
{
int a=5,b=3,s,c; /* Local definitions */
/* Statements
clrscr();
s=sq(a);
c=cube (b);
printf("Square value of 5 is %d\n",s);
printf("Cube value of 3 is %d",c);
} /* main */
OUTPUT
Square value of 5 is
25
Cube value of 3 is 27
EXPLANATION:
The body of the program square and cube value is accessed and prints results
using macro substitutions.
iii) Nested macros
The
macros defined within another macro called nested macros.
Example:
#define A 5
#define B A+2
The
preprocessor directives are
#include:
Used
to include a file.
#define:
Used to define symbolic constant
#ifdef:
Used to test macro definition
#else:
Used to specify alternative in #ifdef
#undef:
Used to undefine a macro.
a)
Every preprocessor must start with # symbol
b)
The preprocessor is always placed before main() function
c)
The preprocessor cannot have termination with semicolon.
d)
There is no assignment operator in #define statement
e)
The conditional macro must be terminated (#ifdef, #endi).
Example 1: Program to
print the values using macro substitution.
/* Program to illustrate preprocessor */
#include<stdio.h>
#include <conio.h>
#define VAL 40 /* macro substitution */
#undef VAL
#define VAL 40
void main()
{
printf ("The
value is: %d\n", VAL);
} /* main */
OUTPUT
The value is: 40
EXPLANATION:
This program displays the value using printf() statement which is defined in
preprocessor.
Example 2: Program to
print value and text using preprocessing.
/* Program to demonstrate on preprocessing */
#include <stdio.h>
#include<conio.h>
#define VAL 35 /* macro
substitution */
#define HELLO "HELLO" /* macro substitution */
void main()
{
int res;
res = VAL‒5;
printf (“Res =VAL‒5:
Res = %d\n", res);
printf
("HELLO");
getch();
} /* main */
OUTPUT:
Res= VAL‒5: Res = 30
HELLO
EXPLANATION:
This program prints the data values which are previously defined under
preprocessor directives.
Example 3: Program to
print the value using preprocessors.
/* Program to illustrate preprocessors*/
#define USD 1 /* macro substitution */
#define UKP 1
#include <stdio.h>
#ifdef USD
#define currency_rate 46 /* macro substitution */
#endif
#ifdef UKP
#define currency_rate 100 /* macro substitution */
#endif
void main()
{
int rs; /* Local
definition */
rs = 10*
currency_rate;
printf ("The
value of rs is: %d\n", rs);
getch();
} /* main */
OUTPUT:
The value of rs is:
1000
EXPLANATION:
This program prints equivalent rupees value for the dollar amount, the currency
rate and all defined as preprocessor.
Computer Programming C: UNIT V: File Operations : Tag: Computer Science : C Programming - The 'C' Preprocessor
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation