C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
1. The Preprocessor and Header File
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
21
1
2. Outline
What is Preprocessor?
Preprocessor Directives
Preprocessor Directives Categories
Macro Expansion
File Inclusion
Conditional compilation
What is a Header File?
Why Header Files are used?
3. What is Preprocessor?
The preprocessor is a program that processes the
source code before it passes through the compiler.
Produce a source code file with the preprocessing
commands properly sorted out.
4. Preprocessor Directives
Preprocessor commands are known as directives.
Preprocessor provides certain features.
These features are also known as preprocessor
directives.
Preprocessor directives start with # sign.
#include <stdio.h>
5. Preprocessor Directives (cont..)
Preprocessor directives can be placed any where in
the source program.
Note: Place it at start of the program.
Each preprocessor directive must be on it’s own line.
7. Preprocessor Directives Categories
Preprocessor directives can be divided into three
categories:
1. Macro Expansion
2. File Inclusion
3. Conditional compilation (also known as compiler
control directives)
8. 1. Macro Expansion
#define directive is known as macro expansion.
Definition:
General Form:
#define macro_template macro_expansion
#define macro_name char_sequence
9. 1. Macro Expansion (cont..)
Preprocessor search for macro definition.
After finding #define directive it search entire
program for macro_template.
Replace each macro_template with
macro_expansion.
Best Practice: Use capital letters for macro
template.
Do not use semicolon ‘ ; ’
10. Why Macro Expansion is used?
To write efficient programs.
To increase readiabiality of programs.
Variable vs macro_template
Compiler can generate faster and compact code for
constant than it can for variables.
When you are dealing with a constant, why use
variable.
A variable may change in the program.
11. 2. File Inclusion
causes one file to be included in another.
<filename> : search the directory on current
directory only.
“filename” : search the directory on current directory
and specified directories as specified in the include
search path.
12. Why File Inclusion is used?
Divide a program in multiple files.
Each file contains related functions.
Some functions or macros are required in each
program
Put them in a file (Library).
Include them in program that need them.
Nested includes: Included file may have more included
files in it.
13. 3. Conditional Compilation/ Compiler
Control Directives
Write single program to run on different
environments.
#ifdef – if defined
#endif – end if
#else – else
#ifndef – in not defined
#if – if
#elif – else if
14. Where Conditional Compilation is used?
To avoide multiple declaration error
Instead of comments(Nested comments not allowed)
Run the same code on different environment.
15. What is a Header File?
A header file is a file with extension .h which contains
C function declarations and macro definitions to be
shared between several source files.
There are two types of header files:
1. The files that the programmer writes and
2. The files that comes with your compiler.
16. Why Header Files are used?
Including a header file is equal to copying the content
of the header file but we do not do it because it will
be error-prone and it is not a good idea to copy the
content of a header file in the source files, especially if
we have multiple source files in a program.
17. The include syntax
Both the user and the system header files are included
using the preprocessing directive #include. It has the
following two forms −
#include <file>
#include “file”
Example:
#include<stdio.h>
#include<string.h>