[go: up one dir, main page]

0% found this document useful (0 votes)
69 views8 pages

Preprocessor Preprocessor: List of Preprocessor Commands

The preprocessor is a program that processes preprocessor directives in a C program before compilation. Preprocessor directives begin with the # symbol and are never terminated with a semicolon. Common preprocessor directives include #include to add header files, #define to define macros, #undef to undefine macros, #if-#else-#endif and #elif for conditional compilation, and #ifdef and #ifndef to check if a macro is defined. The preprocessor allows inclusion of files, macro definition and conditional compilation to customize the compilation of C programs.

Uploaded by

balakrishna3283
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)
69 views8 pages

Preprocessor Preprocessor: List of Preprocessor Commands

The preprocessor is a program that processes preprocessor directives in a C program before compilation. Preprocessor directives begin with the # symbol and are never terminated with a semicolon. Common preprocessor directives include #include to add header files, #define to define macros, #undef to undefine macros, #if-#else-#endif and #elif for conditional compilation, and #ifdef and #ifndef to check if a macro is defined. The preprocessor allows inclusion of files, macro definition and conditional compilation to customize the compilation of C programs.

Uploaded by

balakrishna3283
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/ 8

PREPROCESSOR

Preprocessor
 Preprocessor directive is an instruction given to the compiler before the execution of actual
program.
 Preprocessor directive are processed by a program known as preprocessor.
 Preprocessor is a program which perform before the compilation.
 It is placed before the main().
 It begins with a # symbol.
 They are never terminated with a semicolon.
List of Preprocessor commands:
1. #include
2. #define
3. #undef
4. #if -#else-#endif
5. #elif
6. #ifdef
7. #ifndef

1 : The #include Preprocessor Directive

The #include preprocessor directive is used to include another source file in our source code. This
is commonly used to include header files in our program.

Syntax of #include preprocessor directive

#include "filename.h"
or
#include <filename.h>

The source file to be added must be enclosed within double quotes (" ") or angle brackets (< >) . If
the file is enclosed in double quotes the search will be done in current directory. If the file is
enclosed in angle brackets the search will be done in standard directories (include directory)
where the libraries are stored.

1
Example:
#include <stdio.h>
#include "main.h"
#include "win/display.c"
Here, file "stdio.h" is a standard header file, "main.h" and "win/display.c" is custom C files.

2 : #define preprocessor directive

#define preprocessor directive is the most useful preprocessor directive in C language.


preprocessor processes the defined name and replace each occurrence of a particular
string/defined name (macro name) with a given value (micro body).

Syntax to define a MACRO using #define

#define macro_name value

Example :
#define PI 3.142
#define HUNDRED 100

Rules for defining a symbolic constant :

1 : #sign must be the first character in the line.


2 : No blank spaces between #sign and define word.
3 : A blank space is required between the #define and symbolic-name and between the symbolic-
name and a constant value.
4 : There is no semicolon at the end of #define statement.
5 : The Symbolic name should not be assigned any other value with the program.
6 : Symbolic names have the same form as variable
7 : Symbolic names are written in CAPITALS to distinguish them from normal variables and
defined in the beginning of the program.

2
PPS NOTES_UNIT-III_CH SUBBA REDDY
Macros using #define :
#include <stdio.h>
// Macro definition
#define TRUE 1
#define FALSE 0
int main()
{
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
return 0;
}
The #define Preprocessor Directive as Function
The #define preprocessor directive can be used as inline function which is replaced at compile
time. The definition of macro accepts an argument and that argument is replaced by the actual
argument found in program.
Syntax :

#define macro_name(parameters) value

Example of #define preprocessor as function


#include<stdio.h>
#define SQUARE(x) x*x
void main()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
printf("\nThe square is : %d",SQUARE(num));
}

Output :
Enter any number : 5
The square is : 25

3
PPS NOTES_UNIT-III_CH SUBBA REDDY
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 modified.
__DATE__ : The current date as a character literal in "MMM DD YYYY" format.
__TIME__ : The current time as a character literal in "HH:MM:SS" format.
__FILE__ : This contains the current filename as a string literal.
__LINE__ : This contains the current line number as a decimal constant.
__STDC__ : Defined as 1 when the 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 );
}
3 : #undef preprocessor directive
We use #undef directive to remove a defined macro. We generally un-define a macro, when we do
not require or want to redefine it. To redefine a macro, we need to undefined the macro and
redefine it.
Syntax:
#undef MACRO_NAME

Where MACRO_NAME is the macro to remove.

4
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int main()
{
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
// Undefine a previously defined macro
#undef TRUE
#undef FALSE
// Re-define macro values
#define TRUE 0
#define FALSE 1
printf("\nMacro values are redefinition\n");
printf("TRUE: %d\n", TRUE);
printf("FALSE: %d\n", FALSE);
return 0;
}
4 : The #if-#else-#endif Preprocessor Directive

The #if preprocessor directive takes condition in parenthesis, if condition is true, then the
statements given between #if and #else will get execute. If condition is false, then statements
given between #else and #endif will get execute.
Syntax of #if-#else-#endif Preprocessor Directive

#if(condition)
----------
----------
#else
----------
----------
#endif

5
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example of #if-#else-#endif Preprocessor Directive

#include<stdio.h>
#define MAX 45
void main()
{
#if MAX > 40
printf("Yes, MAX is greater then 40.");
#else
printf("No, MAX is not greater then 40.");
#endif
}

5 : The #elif Preprocessor Directive

The #elif preprocessor directive is similar to if-else ladder statement. It is used for checking
multiple conditions, if the first condition will not satisfy, compiler will jump to #else block and
check other condition is true or not and so on.

Syntax of #if-#elif-#else-#endif Preprocessor Directive

#if(condition)
----------
----------
#elif(condition)
----------
----------
#elif(condition)
----------
----------
#else
----------
----------
#endif

6
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example of #if-#elif-#else-#endif Preprocessor Directive

#include<stdio.h>
#define MKS 65
void main()
{

#if MKS>=78
printf("\nGrade A");

#elif MKS>=50
printf("\nGrade B");

#elif MKS>=25
printf("\nGrade C");

#else
printf("\nGrade D");

#endif

6 : The #ifdef Preprocessor Directive

The #ifdef preprocessor directive is used to check whether the macro-name is previously defined
or not, if defined then the statements given between #ifdef and #else will get execute.

Syntax of #ifdef preprocessor directive

#ifdef macro-name
----------
----------
#else
----------
----------
#endif

7
PPS NOTES_UNIT-III_CH SUBBA REDDY
Example of #ifdef preprocessor directive

#include<stdio.h>
#define MKS 65
void main()
{
#ifdef MONTH
printf("\nMONTH is defined.");
#else
printf("\nMONTH is not defined.");
#endif
}

7 : The #ifndef Preprocessor Directive

The #ifndef preprocessor directive is used to check whether the macro-name is previously defined
or not, if not defined then the statements given between #ifndef and #else will get execute.
Syntax of #ifndef preprocessor directive
#ifndef macro-name
----------
----------
#else
----------
----------
#endif

Example of #ifndef preprocessor directive


#include<stdio.h>
#define MKS 65
void main()
{
#ifndef MAX
printf("\nMAX is not defined.");
#else
printf("\nMAX is defined.");
#endif
}
Note: The #ifdef and #ifndef can not use the #elif preprocessor directive.

8
PPS NOTES_UNIT-III_CH SUBBA REDDY

You might also like