View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12933 Members
Join this group to post and comment.
aarthivg
aarthivg • Jun 12, 2012

'Pre Processor' - What Is It?

compiler does two jobs pre-processor and translator. what does pre processor do
As far as I know, a program is passed to the pre-processor before it is executed or in other words before its processing begins. The pre-processor is involved in the task of linking the libraries. For example, consider a C program

#include
int main()
{
printf("Hi there");
}

#include is a pre processor there which helps in including the header file in the program before the execution begins. If the header file is not included then printf will have no meaning in the program as the definition of printf is in stdio.h header file. Same is the case with including string.h for using string handling functions like strlen(), strcat() etc.
aarthivg
aarthivg • Jun 12, 2012
In the book written by Gilberg, It states that preprocessor reads the source code and prepares it for the translator. While preparing, it scans for the special code called preprocessor commands.
Nick_Sharma, this preprocessor command is what you say as, checking for header files, whether it is included in the program or it helps to include header file.
lovejeet
lovejeet • Jun 12, 2012
the main task of compiler is translation. that is, compiler is a type of translator which translates high level language into machine language. now, according to the gilberg in his book, the 1st phase of compiling is lexical analysis(there are seven such phases). the lexical analysis reads the source code, scans it and arranges it in tokens. so what preprocessor commands are the commands included in the program to include header file and other files or to define macros. correct me if i am wrong.
aarthivg
aarthivg • Jun 12, 2012
lovejeet
the main task of compiler is translation. that is, compiler is a type of translator which translates high level language into machine language. now, according to the gilberg in his book, the 1st phase of compiling is lexical analysis(there are seven such phases). the lexical analysis reads the source code, scans it and arranges it in tokens. so what preprocessor commands are the commands included in the program to include header file and other files or to define macros. correct me if i am wrong.
what is lexical analysis,tokens and macros?

I have just started learning 'c' (only two days old😉 ). So please try to be in simple words😀
Lexical analysis divides the code in simple parts like c=a+b can be divided as a combination of tokens where c,=,a,+,b are called tokens or lexemes.

Macro is another type of pre processor. As #include is used for including files, macro is used to have an alternate definition or kind of substitution .
#define pre processor is used for defining a macro
#define PI 3.14
signifies that wherever you mention PI in your program, it will be replaced by 3.14 at the time of calculation.
A compiler has following phases:
Lexical Analysis, Syntax Analysis, Semantics Analysis, Intermediate Code generator, Code Optimizer, Code Generator.

Share this content on your social channels -