Unit 5 C Language
Unit 5 C Language
Preprocessors are programs that process the source code before compilation. Several
steps are involved between writing a program and executing a program in C. Let us
have a look at these steps before we actually start learning about Preprocessors.
You can see the intermediate steps in the above diagram. The source code written by
programmers is first stored in a file, let the name be “program.c“. This file is then
processed by preprocessors and an expanded source code file is generated named
“program.i”. This expanded file is compiled by the compiler and an object code file
is generated named “program.obj”. Finally, the linker links this object code file to
the object code of the library functions to generate the executable file
“program.exe”.
Preprocessor Directives in C
Preprocessor programs provide preprocessor directives that tell the compiler to
preprocess the source code before compiling. All of these preprocessor directives
begin with a ‘#’ (hash) symbol. The ‘#’ symbol indicates that whatever statement
starts with a ‘#’ will go to the preprocessor program to get executed. We can place
these preprocessor directives anywhere in our program.
The following table lists all the preprocessor directives in C:
Preprocessor Directives Description
These preprocessors can be classified based on the type of function they perform.
Types of C Preprocessors
There are 4 Main Types of Preprocessor Directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives
1. Macros
In C, Macros are pieces of code in a program that is given some name. Whenever
this name is encountered by the compiler, the compiler replaces the name with the
actual piece of code. The ‘#define’ directive is used to define a macro.
A macro is a segment of code which is replaced by the value of macro. Macro is defined by
#define directive. There are two types of macros:
1. Object-like Macros
2. Function-like Macros
Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used to represent
numeric constants.
Syntax
#define PI 3.14
#define LIMIT 5
where after preprocessing, the token will be expanded to its value in the program.
Example 1
#include <stdio.h>
// macro definition
#define PI 3.14
void main()
{
float r=2.5, area;
clrscr();
area=PI*r*r;
printf(“\nThe area of circle is :%d”,area);
getch();
}
Example 2
#include <stdio.h>
// macro definition
#define LIMIT 5
void main()
{
for (int i = 1; i < = LIMIT; i++)
{
printf("%d \n", i);
}
getch();
}
Note There is no semi-colon (;) at the end of the macro definition. Macro definitions do not
need a semi-colon to end.
Function-like Macros
The function-like macro looks like function call.
We can also pass arguments to macros. Macros defined with arguments work
similarly to functions.
Example
#define fun1(a, b) a + b
#define fun2(r) r * r
#include <stdio.h>
void main()
{
int length = 10, breath = 5, area;
getch()
}
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("File :%s\n", _ _FILE_ _ );
printf("Date :%s\n", _ _DATE_ _ );
printf("Time :%s\n", _ _TIME_ _ );
printf("Line :%d\n", _ _LINE_ _ );
getch();
Output:
File :simple.c
Date :May 27 2024
Time :12:28:46
Line :6
2. File Inclusion
This type of preprocessor directive tells the compiler to include a file in the source
code program. The #include preprocessor directive is used to include the header
files in the C program.
There are two types of files that can be included by the user in the program:
Standard Header Files
The standard header files contain definitions of pre-defined functions like printf(),
scanf(), etc. These files must be included to work with these functions. Different
functions are declared in different header files.
Syntax
#include <file_name>
where file_name is the name of the header file to be included. The ‘<‘ and ‘>’
brackets tell the compiler to look for the file in the standard directory.
User-defined Header Files
When a program becomes very large, it is a good practice to divide it into smaller
files and include them whenever needed. These types of files are user-defined header
files.
Syntax
#include "filename"
The double quotes ( ” ” ) tell the compiler to search for the header file in the source
file’s directory.
3. Conditional Compilation
Conditional Compilation in C directives is a type of directive that helps to
compile a specific portion of the program or to skip the compilation of some
specific part of the program based on some conditions. There are the
following preprocessor directives that are used to insert conditional code:
1. #if Directive
2. #ifdef Directive
3. #ifndef Directive
4. #else Directive
5. #elif Directive
6. #endif Directive
#endif directive is used to close off the #if, #ifdef, and #ifndef opening
directives which means the preprocessing of these directives is completed.
Syntax
#ifdef macro_name
// Code to be executed if macro_name is defined
#ifndef macro_name
// Code to be executed if macro_name is not defined
#if constant_expr
// Code to be executed if constant_expression is true
#elif another_constant_expr
// Code to be excuted if another_constant_expression is true
#else
// Code to be excuted if none of the above conditions are true
#endif
#include <stdio.h>
// defining PI
#define PI 3.14159
void main()
{
clrscr();
#ifdef PI
printf("PI is defined\n");
#elif defined(SQUARE)
printf("Square is defined\n");
#else
#error "Neither PI nor SQUARE is defined"
#endif
#ifndef SQUARE
printf("Square is not defined");
#else
Printf("Square is defined");
#endif
getch();
}
Output
PI is defined
Square is not defined
4. Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
1. #undef Directive
2. #pragma Directive
1. #undef Directive
The #undef directive is used to undefine an existing macro. This directive
works as:
#undef LIMIT
Using this statement will undefine the existing macro LIMIT. After this
statement, every “#ifdef LIMIT” statement will evaluate as false.
Example
The below example demonstrates the working of #undef Directive.
#include <stdio.h>
// defining MIN_VALUE
#define MIN_VALUE 10
void main()
{
getch();
}
Output
Min value is: 10
Min value after undef and again redefining it: 20
2. #pragma Directive
This directive is a special purpose directive and is used to turn on or off
some features. These types of directives are compiler-specific, i.e., they vary
from compiler to compiler.
Syntax
#pragma directive
| Bitwise OR operator
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 1
For example,
Bitwise OR operator
The bitwise OR operator is represented by a single vertical sign (|). Two integer operands
are written on both sides of the (|) symbol. If the bit value of any of the operand is 1,
then the output would be 1, otherwise 0.
For example,
Example
#include <stdio.h>
void main()
{
int a=23,b=10; // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
}
Bitwise exclusive OR operator
Bitwise exclusive OR operator is denoted by (^) symbol. Two operands are written on
both sides of the exclusive OR operator. If the corresponding bit of any of the operand
is 1 then the output would be 1, otherwise 0.
For example,
#include <stdio.h>
void main()
{
int a=12,b=10; // variable declarations
printf("The output of the Bitwise exclusive OR operator a^b is %d",a^b);
}
For example,
#include <stdio.h>
void main()
{
int a=8; // variable declarations
printf("The output of the Bitwise complement operator ~a is %d",~a);
}
o Left-shift operator
o Right-shift operator
Left-shift operator
Operand << n
Where,
#include <stdio.h>
void main()
{
int a=5; // variable initialization
printf("The value of a<<2 is : %d ", a<<2);
}
Right-shift operator
Operand >> n;
#include <stdio.h>
void main()
{
int a=7; // variable initialization
printf("The value of a>>2 is : %d ", a>>2);
}