Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 30

PREPROCESSOR

C PREPROCESSOR
C Preprocessor

 C Preprocessor is a program that processes our program


before it is passed to the compiler.

 Preprocessor is such a great convenience that virtually


all C programmers rely on it.
Feature of C preprocessor
 Before a C program is compiled, it is passed through
another program called preprocessor.

 The C program known as ‘Source Code’.

 The preprocessor works on the source code and


creates ‘Expanded source Code’ as per the preprocessor
directive used in the source code.

 Preprocessor directive begins with a # symbol.


Contd…
Following preprocessor directives may be used in he
source code:
a) Macro expansion
b) File inclusion
c) Conditional compilation
d) Miscellaneous directives
Let us understand these preprocessor directive one by
one.
Macro Expansion
 Take a look at the following program:
Cont..
In the statement

 PI is called ‘macro template’, whereas, 3.1428 is called macro


expansion.
 During preprocessing, every macro template gets replaced with its
corresponding macro expansion.
Macro templates are written in capital letters.
 This makes it easy for programmers to identify macro templates when
reading through the program.
Why use #define at all?
• Suppose the constant 3.1428 appears many times in your program.
 you may wish to change all these values with a more accurate
3.142857.
Ordinarily, you would need to go through the program and manually
change each occurrence of the constant.

Cont…
if you have defined PI in a # define directive, you only
one change, in the #define directive itself:

The change will be made automatically to all


occurrences of PI during preprocessing.
 With large programs, macro definitions are almost
Indispensable.
 Since the compiler can generate faster and more
compact code for constants than it can for variables.
Macros with Arguments
Macros can have arguments, just as functions can.

On execution, the program produces the following output:

Thus a = AREA(r1) would be replaced with


a = (3.14 * r1 *r1)
File Inclusion
The next preprocessor directive is file inclusion.
 It cause one file to be included in another and looks like this:

 It simply causes the entire contents of filename to be inserted


into the source code where we have used #include.
It is common for the files that are to be included to have a .h
extension.
This extension stands for ‘header file’, because it contains
statements which when included go to the head of your program.
Example: prototypes of all mathematics related functions are
stored in the header file ‘math.h’, prototypes of console
input/output functions are stored in the header file ‘conio.h’, and so
on.
Cont..
There exist two ways to write #include statement. There are:

# include “mylib.h” This command would look for the file


mylib.h in the current directory as well
as the specified list of directories as
mentioned in the include search path that
might have been setup.

# include <mylib.h> This command would look for the file


mylib.h in the specified list of directories
only.
#if and #elif Directives
The #if directive can be used to test whether an expression
evaluates to a non-zero value or not.
If the result of the expression is non-zero, then subsequent lines
upto a #eles, #elif or #endif are compiled, otherwise they are
skipped.
 A simple example of #if directive is shown below:

 TEST <= 5 evaluates to ture, then statements 1 and 2 are complied,


otherwise statements 4 and 5 are complied.
Miscellaneous Directives
There are two more preprocessor directives available, through they
are not very commonly used. They are:

# pragma Directive

 This directive is another special- purpose directive that you can


use to turn on or off certain features.

 pragmas vary from one build tool to another.

(a) # pragma startup and # pragma exit:


These directives allow us to specify functions that are called upon
program startup (before main()) or program exit(just before the
program terminates).
Cont..
Their usage is as follows:

And here is the output of the program


Contd…
#include<stdio.h>
void first();
void last();
#pragma startup first
#pragma exit last
int main()
{
 printf("\nWithin main..");
 return 0;
}
void first()
{
 printf("\nWithin First..");
}
void last()
{
 printf("\nWithin Last..");
}
Output:
Within First..
Within main..
Within Last..
#pragma warn Macro Directive in C
In c there are many warning messages which can be on
or off with help of #pragma warn.
Syntax :
#pragma warn +xxx
 #pragma warn –xxx
 #pragma warn .xxx
Where
 + means on
 - means off

 . means on/off (toggle)

xxx is indicate particular warning code in thee alphabet.


Example :  rvl  warning code means function should
return a value.
Contd…
#include<stdio.h>
#pragma warn –rvl
int main()
{
printf("It will not show any warning message");
}
Explanation :
-rvl mean suppress warning message .
rvl means : “Function should return a value“.
We have specified return type of main as “Integer” but we
are not going to return a value.
Usually this program will show warning message.
We have suppressed warning already (-rvl) so we won’t get
any warning message.
Cont…
(b)# pragma warm: Directive tells the compiler whether
or not we want to suppress a specific warning.
 Usage of this pragma is shown below.
Cont…
If you go through the program, you can notice three
problems immediately. These are:

(a) Though promised, f1() does not return a value.

(b) The parameter x that is passed to f2() is not being


used anywhere in f2().

(c) The control can never reach x++ in f3().


The Build Process
There are many steps involved in converting a C program
into an executable form.
Figure shows these different steps along with the files
created during each stage.
However, it is important to understand these steps for two
reasons:

a) Understand the process much better, than just believing


that, some kind of ‘magic’ generates the executable code.

b) If you are to alter any of these steps, then you would know
how to do it.
Linker and Loader
Linker and loader are the utility programs that play a major role
in the execution of a program.
The source code of program passes through compiler, assembler,
linker and loader in the respective order before execution.
The source program is converted to object program by assembler.

On one hand, Linker intakes object codes generated by assembler


and combine them to generate executable module.
So linker is a software that links the object code with additional files
such as header file and create an executable file with .exe extension.
Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
20
Difference between linker and loader
On other hand, loader loads this executable module to the
main memory for execution.

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
21
Cont…
 The steps mentioned in figure in detail.
Cont…
 Figure summarizes the role played by each program
during the build process.
Csharp code compilation process
When you run the C# compiler, it takes your code as an
input, does some processing, and then outputs your
program in intermediate language (IL) code which is saved
in *.exe or *.dll files.

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
24
Csharp code compilation process….contd

The processor can only understand machine codes. CLR is a program


running on your computer that manages the execution of IL code. It uses a
just-in-time, or JIT, compiler to translate the IL code into machine code
(sometimes called "native" code). The JIT compiler is named as such
because it compiles IL code at the moment that the user tries to use it; for
example, when you double-click the .exe file.

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
25
Csharp code compilation process….contd

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
26
Csharp code path
The path your code takes from being written to being run

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
27
Translation Hierarchy

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
28
Contd…Order in which source loads to memory

Faculty of Computing and Informatics, JIT, Jimma University, Jimma, Oromia, Ethiopia
29
THANK YOU

You might also like