- C Programming Basics
- C Tutorial
- C Program Structure
- C Basic Syntax
- C Data Types
- C Constants
- C Variables
- C Operators
- C Ternary Operator
- C Storage Classes
- C Flow of Control
- C Decision Making
- C if if-else Statement
- C switch Statement
- C Loops
- C for Loop
- C while Loop
- C do-while Loop
- C goto Statement
- C break Statement
- C continue Statement
- C Popular Topics
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Recursion
- C Scope Rules
- C Programming Advance
- C Structures
- C Unions
- C Bit Fields
- C Enumerations
- C Input & Output
- C Typedef
- C Preprocessors
- C Type Casting
- C Recursion
- C Error Handling
- C Linked Lists
- C Stacks
- C Queues
- C Binary Trees
- C Header Files
- C File I/O
- C Variable Arguments
- C Memory Management
- C Command Line Arguments
- C Programming Examples
- C Programming Examples
- C Programming Library
- C Standard Library
- C Programming Test
- C Programming Test
- Give Online Test
- All Test List
C Preprocessors
Preprocessor in C language, is a text substitution tool. They instruct the C compiler to do the required pre-processing before actual program compilation.
Here the following table lists all the important preprocessor directives available in C language:
#define | #endif | #ifdef | #line |
#elif | #error | #ifndef | #pragma |
#else | #if | #include | #undef |
C Preprocessors Examples
Read the following code fragments carefully to understand the various types of preprocessor directives one by one.
#define
The #define directive defines an identifier and a character sequence that will be substituted for the identifier each time it is encountered in the source file. Here is an example:
#define MAX 100
#error
The #error directive focuses the compiler to stop compilation. It primarily used for debugging purposes. Here is the general form of the #error directive:
#error error-message
Here error-message is not between double quotes.
#include
The #include directive tells the compiler to read another source file in addition to the one that contains the #include directive.
#include<stdio.h> #include "myheader.h"
#undef and #define
#undef FILE_SIZE #define FILE_SIZE 42
This tells the preprocessor to un-define an existing FILE_SIZE and define it as 42
#ifndef, #define and #endif
#ifndef PRINTMESSAGE #define PRINTMESSAGE "You wish!" #endif
This tells the preprocessor to define PRINTMESSAGE only if PRINTMESSAGE isn't already defined
#ifdef DEBUG /* Your debugging statements here */ #endif
This tells the preprocessor to do the process the statements enclosed if DEBUG is defined. This is useful if you pass the -DDEBUG flag to gcc compiler at the time of compilation. This will define DEBUG, so you can turn debugging on and off on the fly during compilation.
C Predefined Macros
Here is an example program, uses predefined macros available in C language:
#include<stdio.h> #include<conio.h> void main() { clrscr(); printf("File : %s\n", __FILE__ ); printf("Date : %s\n", __DATE__ ); printf("Time : %s\n", __TIME__ ); printf("Line : %d\n", __LINE__ ); getch(); }
Above C program will produce the following output:

C Preprocessor Operators
The C preprocessor offers following operators to help you in creating macros:
C Macro Continuation (\)
You can use macro continuation (\) to continue your macro definition in more than one line. Here is an example:
#define message_for(a, b) \ printf(#a " and " #b ": We love you!\n")
C Stringize (#)
The stringize or number-sign operator ('#'), when used within a macro definition, converts a macro parameter into a string constant. Here is an example:
#include<stdio.h> #include<conio.h> #define message_for(a, b) \ printf("Hello, " #a ".\nWelcome to " #b ) void main(void) { clrscr(); message_for(Sir, codescracker.com); getch(); }
Here is the output of this C program:

The defined() Operator
The preprocessor defined operator in C, used in constant expressions to determine if an identifier is defined using the #define. Here is an example:
#include<stdio.h> #include<conio.h> #if !defined (PRINTMESSAGE) #define PRINTMESSAGE "Welcome to codescracker.com" #endif void main(void) { clrscr(); printf("Here is the message:\n%s\n", PRINTMESSAGE); getch(); }
Here is the sample output of the above C program:

C Parameterized Macros
Preprocessor is the ability to simulate functions using parameterized macros. Here is an example:
int square(int num) { return num * num; }
We can rewrite above code using a macro like this:
#define square(num) ((num) * (num))
C Parameterized Macro Example
Here is an example, uses parameterized macro in C:
/* C Preprocessor - C Parameterized Macro - Example Program */ #include<stdio.h> #include<conio.h> #define FINDMAX(num1, num2) ((num1) > (num2) ? (num1) : (num2)) void main(void) { clrscr(); printf("Max between 200 and 100 is %d\n", FINDMAX(100, 200)); getch(); }
Here is the sample output produced by the above C program:

« Previous Tutorial Next Tutorial »