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

18. Processors-in-C

The document provides an overview of C Preprocessors, which transform code before compilation using directives that start with the '#' symbol. It details various types of preprocessor directives, including macros, file inclusion, and conditional compilation, along with examples and predefined macros. Additionally, it explains the purpose of less common directives like #undef and #pragma.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

18. Processors-in-C

The document provides an overview of C Preprocessors, which transform code before compilation using directives that start with the '#' symbol. It details various types of preprocessor directives, including macros, file inclusion, and conditional compilation, along with examples and predefined macros. Additionally, it explains the purpose of less common directives like #undef and #pragma.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C Programming

Topperworld.in

C Preprocessors

 The C Preprocessor is a micro processor that is used by compiler to


transform your code before compilation.
 It is called micro preprocessor because it allows us to add macros.
 All preprocessor directives starts with hash # symbol.

 Let's see a list of preprocessor directives.


 #include
 #define
 #undef
 #ifdef
 #ifndef
 #if
 #else

©Topperworld
C Programming

 #elif
 #endif
 #error
 #pragma

Types of Preprocessor Directives:


1. Macros
2. File Inclusion
3. Conditional Compilation
4. Other directives

1. Macros
 A macro is a segment of code which is replaced by the value of macro. Macro
is defined by #define directive.
 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 macroThere are two types of
macros:

1. Object-like Macros
2. Function-like Macros

 Object-like Macros
The object-like macro is an identifier that is replaced by value. It is widely used
to represent numeric constants.

 Function-like Macros

The function-like macro looks like function call.

©Topperworld
C Programming

C Predefined Macros
ANSI C defines many predefined macros that can be used in c program.

No. Macro Description

1 _DATE_ represents current date in "MMM DD YYYY" format.

2 _TIME_ represents current time in "HH:MM:SS" format.

3 _FILE_ represents current file name.

4 _LINE_ represents current line number.

5 _STDC_ It is defined as 1 when compiler complies with the ANSI


standard.

Example:

#include <stdio.h>

int main() {

printf("File :%s\n", __FILE__ );


printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("ANSI :%d\n", __STDC__ );

When the above code in a file test.c is compiled and executed, it produces the following
result −
Output:
File :test.c
Date :Jun 2 2012
Time :03:36:24
Line :8
ANSI :1

©Topperworld
C Programming

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.

3. Conditional Compilation
 Conditional Complation 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:
 #if Directive
 #ifdef Directive
 #ifndef Directive
 #else Directive
 #elif Directive
 #endif Directive

4. Other Directives
Apart from the above directives, there are two more directives that are not
commonly used. These are:
 #undef Directive
 #pragma Directive

1. #undef Directive

The #undef directive is used to undefine an existing macro.

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.

©Topperworld

You might also like