0% found this document useful (0 votes)
4 views19 pages

Unit II - Preprocessor

The document covers Unit-2 of a programming course, focusing on pre-processor directives in C, including their purpose and types such as inclusion of header files, macro expansion, conditional compilation, and line control. It explains how these directives work, provides examples of macro substitution, predefined macros, and conditional compilation, and discusses the syntax for including files and controlling line numbers. Overall, it serves as a comprehensive guide to understanding and utilizing pre-processor directives in C programming.

Uploaded by

Tanishq Ikhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Unit II - Preprocessor

The document covers Unit-2 of a programming course, focusing on pre-processor directives in C, including their purpose and types such as inclusion of header files, macro expansion, conditional compilation, and line control. It explains how these directives work, provides examples of macro substitution, predefined macros, and conditional compilation, and discusses the syntax for including files and controlling line numbers. Overall, it serves as a comprehensive guide to understanding and utilizing pre-processor directives in C programming.

Uploaded by

Tanishq Ikhar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course: Fundamentals of Programming

Course Code: 24CS01TP0101

UNIT-2

P ro f . N I K I TA K ATA R IYA
D E PA RTM EN T O F C S E
R A M D EOBA BA U N I V E R S IT Y, N A G P U R
Contents
UNIT-II:
➢Switch case statement

➢ Loops

➢Pre-processor Directives
Introduction- Pre-processor Directives
➢Preprocessors are programs that process the source code before compilation.

➢C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-
processing before the actual compilation.

➢The C preprocessor is a macro processor that is used automatically by the C compiler to


transform your program before actual compilation.

➢It is called a macro processor because it allows you to define macros, which are brief
abbreviations for longer constructs.
Pre-processor Directives
➢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.
➢Examples of some preprocessor directives are: #include, #define, #ifndef, etc.
➢Remember that the # symbol only provides a path to the preprocessor, and a command such as
include is processed by the preprocessor program. For example, #include will include the code or
content of the specified file in your program.
how c preprocessor works
Four Major Types of Preprocessor Directives
1. Inclusion of header files - These are files of declarations that can be substituted into your
program.

2. Macro expansion - You can define macros, which are abbreviations for arbitrary fragments of
C code, and then the C preprocessor will replace the macros with their definitions throughout
the program.

3. Conditional compilation - Using special preprocessing directives, you can include or exclude
parts of the program according to various conditions.

4. 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 of where each
source line originally came from.
Examples
Macro substitution directives
➢Macro substitution has a name and replacement text, defined with #define directive.
➢The preprocessor simply replaces the name of macro with replacement text from the place where
the macro is defined in the source code.
➢A macro in C programming is a fragment of code that has been given a name. Whenever the
name is used, it is replaced by the contents of the macro.
➢ Macros are processed by the preprocessor, a step that takes place before the actual compilation
of code.
➢For example, #define PI 3.14159 defines a macro named PI.
No Storage Allocation: Unlike variables, macros don’t allocate storage space. They are only the
replacements done by the preprocessor.
Example1 - Macro substitution directives
#include <stdio.h>
#define PI 3.1415
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = PI*radius*radius;
printf("Area=%.2f",area);
return 0;
}
Example2 - Macro substitution directives
#include <stdio.h>
#define PI 3.1415
#define circleArea(r) (PI*r*r)
int main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius);
area = circleArea(radius);
printf("Area = %.2f", area);
return 0;
}
Predefined Macros
1. ___TIME___ defines the current time using a character literal in “HH:MM: SS”
format.
2. ___STDC___ specifies as 1 when the compiler complies with the ANSI standard.
3. ___TIMESTAMP___ specifies the timestamp “DDD MM YYYY Date HH:MM:
SS”. It is used to define the date and time of the latest modification of the present
source file.
4. ___LINE___ consists of a present line number with a decimal constant.
5. ___FILE___ includes the current filename using a string literal.
6. ___DATE___ shows the present date with a character literal in the “MMM DD
YYYY” format.
Example - Predefined Macros
#include<stdio.h>
int main()
{
printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
Output:-
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("STDC :%d\n", __STDC__ );
return 0;
}
File Inclusion Directives
➢File inclusive Directories are used to include user define header file inside C Program.
➢File inclusive directory checks included header file inside same directory (if path is
not mentioned).
➢File inclusive directives begins with #include
➢If path is mentioned then it will include that header file into current scope.
➢Instead of using triangular brackets we use “Double Quote” for inclusion of user defined header
file.
➢It instructs the compiler to include all specified files.
Ways of including
1 : Including Standard Header Files
header file
#include<filename>
Search File in Standard Library

2 :User Defined Header Files are written in this way


#include"FILENAME“

Search File in Current Library


If not found , Search File in Standard Library
User defined header files are written in this format

Example
#include<stdio.h> // Standard Header File
#include<conio.h> // Standard Header File
#include"myfunc.h" // User Defined Header File
Conditional Compilation
➢Conditional Compilation Directives in C are a set of preprocessing directives that allow us to
include or exclude parts of a program based on certain conditions.
➢These directives are processed before the actual compilation of the code.
➢These are useful for making a program portable across different platforms or including debug
and release versions within the same code base.
Conditional Compilation
1. #if: This directive tests if a certain condition is true. If the condition evaluates to true, the compiler
includes the code between #if and the next #endif, #else, or #elif directive.
2. #ifdef: This checks if a macro is defined. If the macro is defined, the code following #ifdef up to #endif,
#else, or #elif is compiled.
3. #ifndef: Opposite of #ifdef. It checks if a macro is not defined. If the macro is not defined, the code
following #ifndef is compiled.
4. #else: Used with #if, #ifdef, or #ifndef. If the preceding condition is false, the compiler includes the
code following #else.
5. #elif: Short for “else if”. Allows for multiple conditional expressions. If the preceding #if, #ifdef, or
#ifndef is false, and the condition in #elif is true, the code following #elif is compiled.
6. #endif: Marks the end of a conditional compilation block started by #if, #ifdef, #ifndef, #else, or #elif.
7. #define: Used to define macros, which can be used in conditional compilation.
8. #undef: Used to undefine macros.
Exapmle - Conditional Compilation
#include <stdio.h>
#define LEVEL 2
int main()
{
#if LEVEL > 0
printf("Basic level.\n");
#endif
#if LEVEL > 1
printf("Intermediate level.\n");
#endif
#if LEVEL > 2
printf("Advanced level.\n");
#else
printf("Not advanced level.\n");
#endif
return 0;
}
Line Control
➢Whenever we compile a program, there are chances of occurrence of some error in the program.
➢Whenever compiler identifies error in the program it provides us with the filename in which
error is found along with the list of lines and with the exact line numbers where the error is.
➢ This makes easy for us to find and rectify error.
➢However we can control what information should the compiler provide during errors in
compilation using the #line directive.
Syntax
#line number "filename"

number – line number that will be assigned to the next code line. The line numbers of successive
lines will be increased one by one from this point on.
“filename” – optional parameter that allows to redefine the file name that will be shown.
Example - Line Control
#include <stdio.h>
int main()
{
printf("Hello world\n"); // this is line 6
printf("Line: %d\n",__LINE__); // printing line number, line 7
#line 23 //reseting to 23, although next line number is line 10.
printf("Line: %d\n",__LINE__); // printing line number
printf("Line: %d\n",__LINE__);
printf("Line: %d\n",__LINE__);
// let's try with filename (main.c) as well. Output:-
printf( "Line: %d, File: %s\n", __LINE__, __FILE__ ); Hello world
// now we use line to reset filename to "new_filename.c" line number is set to 83 Line: 7
#line 83 "new_filename.c" Line: 23
printf( "Line: %d, File: %s\n", __LINE__, __FILE__ );
return 0; Line: 24
} Line: 25
Line: 29, File: main.c
Line: 83, File: new_filename.c

You might also like