100% found this document useful (1 vote)
21 views24 pages

Pre Processor

Preprocessors in C process source code before compilation, generating an expanded source file that is then compiled into an object file and linked to create an executable. They utilize directives like #include and #define to include files and define macros, respectively, with various types of directives available for different purposes. The document also explains the functionality of predefined macros and provides examples of using preprocessor directives in C programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
21 views24 pages

Pre Processor

Preprocessors in C process source code before compilation, generating an expanded source file that is then compiled into an object file and linked to create an executable. They utilize directives like #include and #define to include files and define macros, respectively, with various types of directives available for different purposes. The document also explains the functionality of predefined macros and provides examples of using preprocessor directives in C programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Preprocessor

Preprocessors are programs that process our


source code before compilation.
There are a number of steps involved
between writing a program and executing a
program in C
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 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.
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 extra code in your program. We can place
these preprocessor directives anywhere in our program.
There are 4 Main Types of Preprocessor Directives:
Macros

File Inclusion

Conditional Compilation

Other directives
C Preprocessor Directives

The C preprocessor is a macro preprocessor (allows you to define macros)


that transforms your program before it is compiled. These transformations
can be the inclusion of header file, macro expansions etc.
All preprocessing directives begin with a # symbol.

Preprocessor directives are executed before compilation.


C program(.C file)

preprocessor

Expanded source
code

compiler

.Obj file
Linker

.EXE file
list of preprocessor directives.

#include
#define
#undef
#ifdef
#ifndef
#if
#else
#elif
#endif
C Macros
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:
Object-like Macros
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. For example:
#define PI 3.14
Here, PI is the macro name which will be replaced by the value 3.14.
Function-like Macros

The function-like macro looks like function call. For example:


#define MIN(a,b) ((a)<(b)?(a):(b))
C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.
No. Macro Description
1 _DATE_ represents current date in "MMM DD YYYY“ format
2 _TIME_ represents current time in "HH:MM:SS" format.
3 _FILE_ represents current file name.
4 _LINE_ represents current line number.
5 _STDC_ It is defined as 1 when compiler complies with the ANSI
standard.C predefined macros example
File: simple.c
#include<stdio.h>
int main(){
printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("STDC :%d\n", __STDC__ );
return 0;
}
Output:
File :simple.c Date :Dec 6 2015 Time :12:28:46 Line :6 STDC :1
C #include

The #include preprocessor is used to include header files to C programs. For example,
#include <stdio.h>Here, stdio.h is a header file. The #include preprocessor directive
replaces the above line with the contents of stdio.h header file.
That's the reason why you need to use #include <stdio.h> before you can use
functions like scanf() and printf().
You can also create your own header file containing function declaration and include it
in your program using this preprocessor directive.
#include "my_header.h"

By the use of #include directive, we provide information to the preprocessor where to


look for the header files. There are two variants to use #include directive.
#include <filename>
#include "filename“

The #include <filename> tells the compiler to look for the directory where system
header files are held. In UNIX, it is \usr\include directory.
The #include "filename" tells the compiler to look in the current directory from where
program is running.
#include directive example
Let's see a simple example of #include directive. In this program, we are including
stdio.h file because printf() function is defined in this file.

#include<stdio.h>
int main(){
printf("Hello C");
return 0;
}
Output:
Hello C
/*Program to find the area of circle using Macro preprocessor command.*/
#include <stdio.h>
#define PI 3.1415
main()
{
float radius, area;
printf("Enter the radius: ");
scanf("%f", &radius); // Notice, the use of PI area = PI*radius*radius; printf("Area=
%.2f",area);
}
C #define
The #define preprocessor directive is used to define constant or micro
substitution. It can use any basic data type.

A macro is a fragment of code that is given a name. You can define a macro in C
using the #define preprocessor directive.

Syntax:
#define token value
Let's see an example of #define to define a constant.
#include <stdio.h>
#define PI 3.14
main() {
printf("%f",PI);
}
Output:
3.140000
Let's see an example of #define to create a macro.

#include <stdio.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
void main() {
printf("Minimum between 10 and 20 is: %d\n", MIN(10,20));
}
Output:
Minimum between 10 and 20 is: 10
C #undef
The #undef preprocessor directive is used to undefine the constant or macro defined by
#define.
Syntax:
#undef token
Let's see a simple example to define and undefine a constant.
#include <stdio.h>
#define PI 3.14
#undef PI
main() {
printf("%f",PI);
}
Output:
Compile Time Error: 'PI' undeclared
#define – This macro defines constant value and can be any of the basic data types.
#include <file_name> – The source code of the file “file_name” is included in the main C
program where “#include <file_name>” is mentioned.

#include <stdio.h>
#define number 3.14
#define letter 'A‘

main()
{
printf("value of number : %f \n", number );
printf("value of letter : %c \n", letter );

Output:
Value of number:3.140000
Value of letter: A
“#ifdef” directive checks whether particular macro is defined or not. If it is defined, “If”
clause statements are included in source file.
Otherwise, “else” clause statements are included in source file for compilation and
execution.
#include <stdio.h>
#define CSED 60

main()
{
#ifdef CSED
printf(“CSED is defined. So, this line will be added in " \
"this C file\n");
#else
printf(“CSED is not defined\n");
#endif

}
OUTPUT: CSED is defined. So, this line will be added in this C file.
#ifndef exactly acts as reverse as #ifdef directive. If particular macro is not defined, “If”
clause statements are included in source file.
Otherwise, else clause statements are included in source file for compilation and
execution
#include <stdio.h>
#define CSED 100
main()
{
#ifndef ITA
{
printf(“ITA is not defined\n");

}
#else
printf(“ITA is already defined in the program”);

#endif

}
“If” clause statement is included in source file if given condition is true.
Otherwise, else clause statement is included in source file for compilation and
execution.

#include <stdio.h>
#define a 200
main()
{
#if(a==100)
printf("This line will be added in this C file since " \
"a \= 100\n");
#else
printf("This line will be added in this C file since " \
"a is not equal to 100\n");
#endif
}
/*program to demonstrate #if ,#else,#endif*/
#include<stdio.h>
#define A 30
#define B 10
main()
{
int res;
#if (A>B)
res=A+B;
printf("sum=%d",res);

#else
res=A-B;
printf("diff=%d",res);
#endif
}

Output: Sum=40.
/*program to demonstrate #elif */
#include<stdio.h>
#define A 10
#define B 10
main()
{
int res;
#if (A>B)
res=A+B;
printf("sum=%d“,res);

#elif (A==B)
res=A*B;
printf("mul=%d“,res);
#else
res=A-B;
printf("diff=%d“,res);
#endif
}
Output: Mul=100.
/*program to demonstrate #ifdef and #ifndef*/
#include<stdio.h>
#define A 30
main()
{
#ifdef A
printf("A is defined\n");
#endif
#ifndef B
printf("B is not defined");
#endif
}

Output:
A is defined.
B is not defined.
This directive undefines existing macro in the program.
The #undef preprocessor directive is used to undefine the constant or macro defined by
#define.

#include <stdio.h>
#define PI 3.14
#undef PI
main() {
printf("%f",PI);
}

OUTPUT:
Compile Time Error: 'PI' undeclared

You might also like