0% found this document useful (0 votes)
24 views4 pages

Macros & Preproccesor

The C preprocessor is a text substitution tool that is run as a separate step before compilation. It handles preprocessing directives like #define, #include, #if, #else, and #endif. #define is used to define macros, which are names that are substituted for code. #include inserts the contents of another file. Predefined macros like __DATE__, __TIME__, __FILE__, and __LINE__ provide information about the compilation.

Uploaded by

ronge7548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Macros & Preproccesor

The C preprocessor is a text substitution tool that is run as a separate step before compilation. It handles preprocessing directives like #define, #include, #if, #else, and #endif. #define is used to define macros, which are names that are substituted for code. #include inserts the contents of another file. Predefined macros like __DATE__, __TIME__, __FILE__, and __LINE__ provide information about the compilation.

Uploaded by

ronge7548
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Features of C pre-processor :

The C Preprocessor is not a part of the compiler, but is a separate step in the
compilation process. In simple terms, a C Preprocessor is just a text substitution tool
and it instructs the compiler to do required pre-processing before the actual
compilation. We'll refer to the C Preprocessor as CPP.

All preprocessor commands begin with a hash symbol (#). It must be the first
nonblank character, and for readability, a preprocessor directive should begin in the
first column. The following section lists down all the important preprocessor
directives −

Sr.No
Directive & Description
.

#define
1
Substitutes a preprocessor macro.

#include
2
Inserts a particular header from another file.

#undef
3
Undefines a preprocessor macro.

#ifdef
4
Returns true if this macro is defined.

#ifndef
5
Returns true if this macro is not defined.

#if
6
Tests if a compile time condition is true.

#else
7
The alternative for #if.

#elif
8
#else and #if in one statement.

#endif
9
Ends preprocessor conditional.

#error
10
Prints error message on stderr.

#pragma
11 Issues special commands to the compiler, using a
standardized method.

Prof.Mane P.V. Page 1


Predefined Macros:

ANSI C defines a number of macros. Although each one is available for use in
programming, the predefined macros should not be directly modified002E

Sr.No
Macro & Description
.

__DATE__
1 The current date as a character literal in "MMM DD YYYY"
format.

2 __TIME__
The current time as a character literal in "HH:MM:SS" format.

3 __FILE__
This contains the current filename as a string literal.

4 __LINE__
This contains the current line number as a decimal constant.

__STDC__
5 Defined as 1 when the compiler complies with the ANSI
standard.

Let's try the following example −

Live Demo

#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__ );

}
Macro in c is defined by the #define directive. Macro is a name given to a piece of
code, so whenever the compiler encounters a macro in a program, it will replace it
with the macro value. In the macro definition, the macros are not terminated by the
semicolon(;).
Syntax of a Macro:
#define macro_name macro_value;

Prof.Mane P.V. Page 2


Example:

#define pi 3.14;

Below we have an example program using macros in c

Example Program:

#include <stdio.h>

#define a 10 //macro definition

int main()

printf("the value of a is: %d", a);

return 0;

From the above code, “a” is a macro name and 10 is the value. Whenever the
compiler encounters a macro name, it will replace it with the macro value.

// C program to illustrate macros

#include <stdio.h>

// Macro definition

#define LIMIT 5

// Driver Code

Prof.Mane P.V. Page 3


int main()

// Print the value of macro defined

printf("The value of LIMIT"

" is %d",

LIMIT);

return 0;

File Inclusion in C

#include causes the contents of another file to be compiled as if they actually


appeared in place of the #include directive. The way this substitution is performed is
simple, the Preprocessor removes the directive and substitutes the contents of the
named file. Compiler allows two different types of #include’s, first is standard library
header include, syntax of which is as follows,

#include <filename.h>
Here, filename.h in angle brackets causes the compiler to search for the file in a
series of standard locations specific to implementation. For example, gcc compiler
on Linux, searches for standard library files in a directory called /usr/include.

Other type of #include compiler supports is called local include, whose syntax is as
follows,

#include "filename.h"

Prof.Mane P.V. Page 4

You might also like