Module V (Part-3)Preprocessor&user defined file
Module V (Part-3)Preprocessor&user defined file
Preprocessors are programs that process the source code before compilation.
A number of steps are 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 Directives in C
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.
Preprocessor
Description
Directives
1. Macros
In C/C++, Macros are pieces of code in a program that is given some name.
Whenever this name is encountered by the compiler, the compiler replaces the
name with the actual piece of code. The ‘#define’ directive is used to define a
macro.
There are two types of files that can be included by the user in the
program:
Syntax
#include <file_name>
where file_name is the name of the header file to be included. The ‘<‘ and ‘>’
brackets tell the compiler to look for the file in the standard directory.
3. Conditional Compilation
Conditional Compilation in C directives is a type of directive that helps to
compile a specific portion of the program or to skip the compilation of some
specific part of the program based on some conditions. There are the following
preprocessor directives that are used to insert conditional code:
1. #if Directive
2. #ifdef Directive
3. #ifndef Directive
4. #else Directive
5. #elif Directive
6. #endif Directive
#endif directive is used to close off the #if, #ifdef, and #ifndef opening
directives which means the preprocessing of these directives is completed.
Syntax
#ifdef macro_name
statement1;
statement2;
statement3;
.
.
.
statementN;
#endif
If the macro with the name ‘macro_name‘ is defined, then the block of
statements will execute normally, but if it is not defined, the compiler will simply
skip this block of statements.
4. Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
1. #undef Directive
2. #pragma Directive
1. #undef Directive
The #undef directive is used to undefine an existing macro. This directive works
as:
#undef LIMIT
Using this statement will undefine the existing macro LIMIT. After this
statement, every “#ifdef LIMIT” statement will evaluate as false.
2. #pragma Directive
This directive is a special purpose directive and is used to turn on or off some
features. These types of directives are compiler-specific, i.e., they vary from
compiler to compiler.
As we all know that files with .h extension are called header files in C. These
header files generally contain function declarations which we can be used in our
main C program, like for e.g. there is need to include stdio.h in our C program to
use function printf() in the program
Header files generally contain definitions of data types, function prototypes and
C preprocessor commands.
Below is the short example of creating your own header file and using it
accordingly.
1. Creating myhead.h : Write the below code and then save the file
as myhead.h or you can give any name but the extension should be .h
indicating its a header file.
1.
#include <stdio.h>
#include "myhead.h"
int main()
{
add(4, 6);
multiply(5, 5);
Added value:10
Multiplied value:25
BYE!See you Soon
Important Points:
The creation of header files are needed generally while writing large C programs so that
the modules can share the function definitions, prototypes etc.
Function and type declarations, global variables, structure declarations and in some
cases, inline functions; definitions which need to be centralized in one file.
In a header file, do not use redundant or other header files; only minimal set of
statements.
Don’t put function definitions in a header. Put these things in a separate .c file.
Include Declarations for functions and variables whose definitions will be visible to the
linker. Also, definitions of data structures and enumerations that are shared among
multiple source files.
In short, Put only what is necessary and keep the header file concised.