0% found this document useful (0 votes)
5 views

Module V (Part-3)Preprocessor&user defined file

Cse-1

Uploaded by

Ishita Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module V (Part-3)Preprocessor&user defined file

Cse-1

Uploaded by

Ishita Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Preprocessors

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.

Examples of some preprocessor directives are: #include, #define, #ifndef, etc.

Remember that the # symbol only provides a path to the preprocessor,


and a command such as include is processed by the preprocessor
program. For example, #include will include the code or content of the
specified file in your program.

Preprocessor
Description
Directives

#define Used to define a macro

#undef Used to undefine a macro

#include Used to include a file in the source code program

Used to include a section of code if a certain macro is defined by


#ifdef
#define
Preprocessor
Description
Directives

Used to include a section of code if a certain macro is not


#ifndef
defined by #define

#if Check for the specified condition

#else Alternate code that executes when #if fails

#endif Used to mark the end of #if, #ifdef, and #ifndef

These preprocessors can be classified based on the type of function they


perform.

Types of C/C++ Preprocessors


There are 4 Main Types of Preprocessor Directives:
1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other 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.

Syntax of Macro Definition

#define token value


2. File Inclusion
This type of preprocessor directive tells the compiler to include a file in the
source code program. The #include preprocessor directive is used to include the
header files in the C program.

There are two types of files that can be included by the user in the
program:

Standard Header Files


The standard header files contain definitions of pre-defined functions
like printf(), scanf(), etc. These files must be included to work with these
functions. Different functions are declared in different header files.

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.

User-defined Header Files


When a program becomes very large, it is a good practice to divide it into
smaller files and include them whenever needed. These types of files are user-
defined header files. These files can be included as:
#include "filename"
The double quotes ( ” ” ) tell the compiler to search for the header file in
the source file’s 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.

- write your own header file in C or user


defined header file -

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.

void add(int a, int b)


{

printf("Added value=%d\n", a + b);

void multiply(int a, int b)

printf("Multiplied value=%d\n", a * b);

2.Including the .h file in other program : Now as we need to include stdio.h as


#include in order to use printf() function. We will also need to include the above
header file myhead.h as #include”myhead.h”. The ” ” here are used to instructs
the preprocessor to look into the present folder and into the standard folder of all
header files if not found in present folder. So, if you wish to use angular brackets
instead of ” ” to include your header file you can save it in the standard folder of
header files otherwise. If you are using ” ” you need to ensure that the header file
you created is saved in the same folder in which you will save the C file using
this header file.

2. Using the created header file :

// C program to use the above created header file

#include <stdio.h>

#include "myhead.h"

int main()
{

add(4, 6);

/*This calls add function written in myhead.h

and therefore no compilation error.*/

multiply(5, 5);

// Same for the multiply function in myhead.h

printf("BYE!See you Soon");

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.

You might also like