C Preprocessor
Build Process:
1. The C programming language is a high-level language, but computers
cannot understand this language directly.
2. Computers are able to execute your programs because the programs that
you write in the C language first go through a ‘build process’ and are
finally converted into an executable code, which can be understood by a
computer.
3. To summarise, various steps are involved from writing a C program to
getting it executed. The combination of these steps is known as the ‘build
process’. These steps are as follows: ○ Preprocessing ○ Compiling ○
Assembling ○ Linking ○ Loading
The detailed ‘build process’ is as follows:
4. The C program that you write in a text editor is known as a ‘source code’.
5. It goes through a preprocessor that expands the source code, after which
you obtain an expanded source code.
6. This expanded source code is then converted into an assembly code by a
compiler.
7. The assembler then converts this assembly code into a relocatable object
code.
8. Finally, the linker combines this relocatable object code with the object
code of library functions, which is used in the code, to form an executable
code
9. Preprocessor Directives:
10. The reprocessor offers several features called ‘preprocessor
directives’. Some of these preprocessor directives are as follows:
1. File inclusion
2. Macro expansion
3. Conditional compilation
File Inclusion:
File inclusion is used to include all the file contents that are used in
the program.
The preprocessor command for file inclusion looks like the following:
#include "filename" or #include This command simply allows all the
contents of the filename to be inserted into the source code at that point
in the program, assuming that the file being included exists.
1. #include”filename” command is used to search the file in the current directory first and
then in the include directory.
2. The #include command searches the file only in the include directory. Is cellaneous
directives
3. Macro Expansion:
4. The #define command is used to define a macro.
Example:
, #define LOWER 5
#include<stdio.h>
int main()
{ printf("%d", LOWER);
}
‘LOWER’ is known as a macro template, and ‘5’ is known as a macro expansion.
1. Every template gets replaced by its expansion.
2. Macros have a global effect.
3. This can be used when a constant value appears at multiple places in the program.
Example:
Suppose you need to write a C program to find the curved surface area, the
total surface area or the volume of a sphere (given its radius). All these formulas
use the value of pi, i.e., 3.14. Now, rather than writing the value of pi (3.14)
multiple times in a single program, you can make use of macro as follows:
#define PI 3.14
1. Macros are of two types, which are as follows:
2. Simple macros ○ Macros with argument
Example:
# include<stdio.h>
# define PI 3.14
# define AREA( x ) PI * x * x
float area ( float ) ;
int main( )
{
float r, a ;
scanf ( "%f", &r ) ;
a = AREA ( r ) ;
printf ( "\n%f", a ) ;
a = area ( r ) ;
printf ( "\n%f", a ) ;
}
float area ( float rr )
Build Process:
1. The C programming language is a high-level language, but computers
cannot understand this language directly.
2. Computers are able to execute your programs because the programs that
you write in the C language first go through a ‘build process’ and are
finally converted into an executable code, which can be understood by a
computer.
3. To summarise, various steps are involved from writing a C program to
getting it executed. The combination of these steps is known as the ‘build
process’. These steps are as follows: ○ Preprocessing ○ Compiling ○
Assembling ○ Linking ○ Loading
The detailed ‘build process’ is as follows:
4. The C program that you write in a text editor is known as a ‘source code’.
5. It goes through a preprocessor that expands the source code, after which
you obtain an expanded source code.
6. This expanded source code is then converted into an assembly code by a
compiler.
7. The assembler then converts this assembly code into a relocatable object
code.
8. Finally, the linker combines this relocatable object code with the object
code of library functions, which is used in the code, to form an executable
code
9. Preprocessor Directives:
10. The reprocessor offers several features called ‘preprocessor
directives’. Some of these preprocessor directives are as follows:
1. File inclusion
2. Macro expansion
3. Conditional compilation
File Inclusion:
File inclusion is used to include all the file contents that are used in
the program.
The preprocessor command for file inclusion looks like the following:
#include "filename" or #include This command simply allows all the
contents of the filename to be inserted into the source code at that point
in the program, assuming that the file being included exists.
1. #include”filename” command is used to search the file in the current directory first and
then in the include directory.
2. The #include command searches the file only in the include directory. Is cellaneous
directives
3. Macro Expansion:
4. The #define command is used to define a macro.
Example:
, #define LOWER 5
#include<stdio.h>
int main()
{ printf("%d", LOWER);
}
‘LOWER’ is known as a macro template, and ‘5’ is known as a macro expansion.
1. Every template gets replaced by its expansion.
2. Macros have a global effect.
3. This can be used when a constant value appears at multiple places in the program.
Example:
Suppose you need to write a C program to find the curved surface area, the
total surface area or the volume of a sphere (given its radius). All these formulas
use the value of pi, i.e., 3.14. Now, rather than writing the value of pi (3.14)
multiple times in a single program, you can make use of macro as follows:
#define PI 3.14
1. Macros are of two types, which are as follows:
2. Simple macros ○ Macros with argument
Example:
# include<stdio.h>
# define PI 3.14
# define AREA( x ) PI * x * x
float area ( float ) ;
int main( )
{
float r, a ;
scanf ( "%f", &r ) ;
a = AREA ( r ) ;
printf ( "\n%f", a ) ;
a = area ( r ) ;
printf ( "\n%f", a ) ;
}
float area ( float rr )