EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Preprocessor Directives in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Preprocessor Directives in C

Preprocessor Directives in C

Introduction to Preprocessor Directives in C

Most often it is made as a misconception that Preprocessors or macros are part of the compilation of the program, but it is totally wrong. Preprocessor directives are the type of macros and a phase before compilation takes place. It can be said that these are some set of instructions given to compiler to perform actual compilation. They are also known as longer construct abbreviations for macros which means the value in macros gets replaced with the segment of codes. Macro is defined by a special symbol and has a symbol starting with “#” Therefore these # define is a kind of special preprocessor followed by the actual compiler. In this topic, we are going to learn about Preprocessor Directives in C.

The preprocessor has a special type of representation for its identification like any preprocessor directive initiates itself by a special symbol of “#” followed by an identifier and then the directive name. Whitespace is also allowed before and after the #. For example, # include.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There are certain fits or facilities which a C Preprocessor can provide:

  • Header files: Inclusion of header files are a way in which declarations can get substituted by program syntax and program body.
  • Expanding Macro: Defining macros are like abbreviating a piece of code which a C preprocessor replaces the macros with their respective definition throughout.
  • Compilation by Conditions: According to various scenarios or various conditions inclusion of certain parts of the program is possible by conditional compilation.
  • Line Control: If you use a program to combine or rearrange someone or more source files into an intermediate file for compilation, you can use line control to inform the compiler of where each source line originated from.

Types of Preprocessor Directives

All types of Preprocessor Directives are as follows:

  1. # define
  2. # include
  3. # ifdef
  4. # undef
  5. #ifndef
  6. # if
  7. #else
  8. #elif
  9. #endif
  10. #error
  11. #pragma

1. #define (Macros)

A macro is a code snippet that is replaced by some value of the code of macro. Any macro is mostly described and defined by its #define directive.

Syntax:

#define token value

There are two types of macros:

  • Function – like macros
  • Object – like macros
Function – like macros

The function like-macro works almost like a function call.

For example:

#define MAX(a,b) ((a)>(b) ? (a): (b))

MAX here is the Macro name.

Example:

Preprocessor Directives in C 1

Output:

Preprocessor Directives in C 2

Object – like macros

Object-like macros are the type of identifier replaced by value. It is mostly used to represent numeric constants.

#define PI 3.1415

Here the value of PI will get substituted by the macro.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Preprocessor Directives in C 3
Output:

Preprocessor Directives in C 4

2. #include

There is some other functionality for the include preprocessor directive. It has its own three variants which replace the code with the current source files code.

Three variants are as follows:

  • #include<file>
  • #include”file”
  • Include anything else

#include<file>

Searches for a file in the defined list of the system or directories as specified then searches for a standard list of system libraries.

#include”file”

This type is used for your own customized header files of the program. A search is made for a file named file first in the current directory followed by system header files and current directories of directory current file.

#include anything

This type of include preprocessor directive is used when none of the other remaining two types of the directive and its arguments don’t fit and satisfy the computation structure.

3. #Ifdef

Checks whether the macro is defined by # define or not. If yes, it will execute the code otherwise it will not.

Syntax:

#ifdef MACRO
{
Code body
}
#endif
#undef:

To cancel the definition of a macro means it is undefined and is preceded with #undef directive.

Syntax:

#undef token

Preprocessor Directives in C 5

Output:

Ifdef token output

4. Ifndef

This processor checks whether #define is defined or not by #define. If yes, it executes the code.

Syntax:

#Ifndef MACRO
//code
#endif
#If

This processor works like an if loop only, it evaluates the expression or condition. If condition id true It will execute the code otherwise not.

Syntax:

#if expression
//code
#endif
#else

The #else preprocessor directive is used to evaluate the expression or condition if the condition of #if is false. It can be used with #if, #elif, #ifdef and #ifndef directives.

Syntax:

#if expression
//if body
#else
//else body
#endif

Example:

Ifndef example

Output:

Ifndef output

5. #Error

As its name suggests Error preprocessor directive is used to indicate an error and then the compiler gives a fatal error if error directive is found and skips the next compilation steps.

Error example

Output:

Error output

6. #pragma

It depends on the compiler as different OS and different machines provide all types of operating system feature which is used by the compiler to offer additional information to the compiler.

Syntax:

#pragma token

Example:

pragma example

Output:

pragma output

Every Preprocessor has its own significance like conditional directive is used to check whether a part of the program is to be taken into consideration based on scenarios or not.

Suppose a program wants to get compiled in a specified environment with the specific config of Operating system but as soon as it goes to that phase of compilation it throws an error or it may give an invalid code merely giving its program the possibility of a big no to link the program and run it while executing. There can be another possibility also where the same source file with two different programs can make a time-consuming consistency check or its immediate data, or prints the values of data with debugging.

Also, these scenarios to be created with computation can also be used to run on one machine by using preprocessing directives.

Conclusion

The output from the C Preprocessor looks a lot like the input, except that all preprocessing directives have been replaced with blank lines or whitespaces. Different files and formats have different syntaxes saying that the start of a new file has been made or indicating a return to a file or processing should be done before compilation.

All the scenarios are used to let others know the power of C preprocessor and how it is evolving with different versions of compiler started with GCC and ANSI standards.

Recommended Articles

This is a guide to Preprocessor Directives in C. Here we discuss the types of Preprocessor Directives with syntax and examples. You may also have a look at the following articles to learn more-

  1. Reverse Number in C
  2. Sorting in C
  3. Hashing Function in C
  4. Continue Statement in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More