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

Preprocessor_in_c

The document explains the role of preprocessor directives in C programming, detailing how they facilitate communication between humans and computers through I/O operations. It covers various types of preprocessor directives, including macros, file inclusion, conditional compilation, and line control, along with their syntax and examples. Additionally, it describes the use of standard header files and user-defined header files in C programs.

Uploaded by

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

Preprocessor_in_c

The document explains the role of preprocessor directives in C programming, detailing how they facilitate communication between humans and computers through I/O operations. It covers various types of preprocessor directives, including macros, file inclusion, conditional compilation, and line control, along with their syntax and examples. Additionally, it describes the use of standard header files and user-defined header files in C programs.

Uploaded by

Ashish Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Preprocessor

Humans and computers communicate using input/output devices.

Programming languages facilitate such communication through I/O operations like reading input from
the keyboard, displaying output on screens, writing to files, printers, etc.

C language provides many functions to perform, read, and write operations for the I/O devices. These
functions are made available in files (usually written with .h extension) referred to, as header files.

Standard I/O library functions are available in a file named stdio.h (standard input output header file).

C language provides a collection of such header files which form the C standard library. These files are
usually available in operating systems like Linux by default.

Programmers can also create their own header files which are usually referred to as user-defined header
files.

To use the functions available in the header file stdio.h, the following line has to be used in a program:

#include <stdio.h>

#include:
The header files are included in a program using the #include directive.

The header files can be included using #include in two ways as follows:

#include <header_file_name.h>: This variant is used to include system header files made available in C
standard library. The compiler searches for the named file in the standard list of system directories.
#include “header_file_name.h”: This variant is commonly used to include user-defined header files. The
compiler searches for the named files only in the local or project-specific paths.

All the preprocessor directives begin with a hash symbol #. It pre-processes the program before the
compilation of the program. The C preprocessor is used automatically by the C compiler to transform
the program before compiling. A series of textual transformations are done on its input and after that, it
will return your C code to the compiler. And after that, the compiler compiles the code. All the
preprocessor directive names start with a hash # which means all the statements starting with a # will
first go to the preprocessor and then get executed.
header file which contains the standard input/output functions like, printf(), scanf(), etc.

There are Mainly four Types of Preprocessor Directives:-

Macros: The Macros directives replace every occurrence of the identifier with a predefined string which
means we are substituting the identifier with a string. Its syntax is as follows:
#define identifier string

let us see an example:

#include <stdio.h>
#define PI 3.1415
main()
{
printf("THE VALUE OF PI IS: %f",PI);
}

Output:

THE VALUE OF PI IS: 3.141500

Explanation: In the above example, the value of PI that is (3.1415 approx) is assigned to the string PI.

File Inclusion: A file inclusion directive is an external file containing function macro definitions, that can
be included using #include directive. The name suggests you need to include any file at the start of the
program. The syntax of this directive is as follows:
#include <file name>

Let us see an example.

#include <stdio.h> // file inclusion directive is here


#define PI 3.1415
main()
{
printf("THE VALUE OF PI IS: %f",PI);
}

Explanation: In the above example, the header file <stdio.h> is used to include the functions like
standard input and output functions.

Conditional Compilation: This C preprocessor offers a feature that is known as conditional compilation
which means it can be used to switch the ON or OFF of a particular line or group of lines in a program.
The "ON" or "OFF" will work for only that line of code. Using it, we can skip any part of the code as per
our needs. Some examples:
#if, #else, #endif, etc..

Let us see an example.

#include <stdio.h>

#define Age 18

int main()
{
#ifdef Age // conditional compilation directives
printf("This person is over %d years old.\n", Age);
#endif // also a conditional compilation directive

printf("So, he is eligible.\n");

return 0;
}

Output:

This person is over 18 years old.


So, he is eligible.

Explanation: In the above example, conditional directive #ifdef is used to apply the condition in the
code, and #endif is used to terminate the code if the condition is satisfied.

Line Control: This preprocessor directive in C provided information about the error or mistakes in a
program. It gives the line number where there is an error in syntax, indentation, etc. Syntax as follows:
Syntax 1
# line-number “file_name”

Syntax 2
# line line-number “file_name”

Let us see an example.

#include <stdio.h>
void function_1();
void function_2();

#pragma startup function_1


#pragma exit function_2

void function_1()
{
printf("Inside the function_1()\n");
}

void function_2()
{
printf("Inside the function_2()\n");
}

int main()
{
void function_1();
void function_2();
printf("Inside the main()\n");

return 0;
}

Output:

Inside the main()

Explanation: In the above example, line control preprocessor directives #pragma is used to switch the
control of line execution of code.

1. #include:
This directive is used to tell the system to include the current file specified in the input, in the rest of the
original file. If the file that has to be included is not found, the compiler throws an error. This directive is
of three types:

#include : This directive is used to include a header file. It searched the entire directory and finds that
particular header file as specified by the user.
#include "file": This directive searched for the header file in the current directory. The directory of the
current input file is called the current directory.
#include anything else: If any of the above two cases failed, then this directive is used.
2. #define:
This preprocessor directive in C comes under Macros. A macro is one when some particular section of
code is substituted by the value of the macro. There are two types of Macros:
Object-like Macros: When the macro is replaced by a value, generally for numerical value. Example:-
Here, "PI" is used with the preprocessor dictie. So the value of 'PI' = 3.1415 will be assigned in the code.
#define PI 3.1415

Function-like Macros: When the macro id is replaced by a function call. Example:


#define MIN(a,b) ((a)<(b)?(a):(b))

3. #undef:
This directive is used to remove the definitions related to a particular macro. The syntax of this directive
is as follows:

#undef token

4. #ifdef:
This preprocessor directive is used to check if a macro is defined earlier or not. If it is defined, then the
code executes normally. The syntax of this directive is as follows:

#ifndef MACRO
//code
#endif

5. #ifndef:
This preprocessor directive is used to check whether a "#define" is defined or not defined by the user. If
it is so, then the code will execute. The syntax of this directive is as follows:

#Ifndef MACRO
//code
#endif
#If

6. #if:
This preprocessor directive is used for evaluating the conditions or expressions. If the condition satisfies,
the code will execute. The syntax of this derivative is as follows:

#if expression
//code
#endif

7. #else:
This preprocessor directive is also used for the evaluation of conditions or expressions. But here it is
used to check if the condition is false, then what will further happen to the flow of code like where the
compiler will shift for the execution of the program. We can use it with #if, #ifdef, #elif, and #ifndef
directives. The syntax of this directive is as follows:

#if expression
//if code
#else
//else code
#endif

8. #elif:
This directive can be used with the #if, #ifdef, or #ifndef directives and the #elif. If the condition is true
then it provides more conditions for the program. The syntax of this directive is as follows:

#elif condition

9. #endif:
This preprocessor directive is used to indicate the end of the directives like #if, #ifdef, or #ifndef
condition.

10. #error:
This preprocessor directive is used to show an error. The compiler shows an error directive and it
immediately stops the further compilation of the program. The syntax of this directive is as follows:

#error error_message

11. #pragma:
This preprocessor directive is used to present some extra information to the compiler. This directive can
provide information such as machine details or the details of the operating system to the compiler. The
syntax of this directive is as follows:

#pragma token

You might also like