EDUCBA

EDUCBA

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

Preprocessor in C

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

Preprocessor in C

Introduction to Preprocessor in C

The preprocessor is a processor which allows you to define abbreviations for longer constructs that can be used in the program instead of many numbers of lines of codes to less number of lines of codes. In C, the preprocessor is not a part of the compiler instead which is used to transform the code before its compiled. This is also called a macro processor because it helps you to define the code in short names called as a macro. In C, preprocessors provide few commands which begin with # (hash symbol). These preprocessor directives contain a set of statements as single macros that are used at the beginning of the program so that we can use it any number of times in the entire program.

How does Preprocessor in C works?

In C programming language, the preprocessor directives are defined using the # hash symbol. In general, when the C programs are written and then saved using .c and such files are then processed by the preprocessor, this expanded file is then compiled and the object file with .obj which are linked with linker which links these object file to generate an executable file with .exe files. So these preprocessor directives are having a set of codes that can be defined with a single name called as a macro that can be used any number of times in the entire program that is defined and declared at the beginning of the program.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of Preprocessor in C

There different types of preprocessor directive are as follows:

1. Macros

As discussed above, macros are a piece of code in which it contains set of statements that do a particular work or contains logic that needs to be used any number of times in the program, then we can just declare this defined macro in the program whenever needed to execute this logic in the program. This is done by the compiler whenever the compiler encounters this macro name in the program then the compiler replaces this macro name with a set of code that is defined at the beginning of the program. This is done using the #define directive to define the macro name.

Let us consider an example of how macro is defined and used in the program.

#define macro_name macro_value

Code:

#include <stdio.h>
#define  MAX 8
int main()
{
printf("To print the numbers using macro definition:\n");
for (int i = 0; i < MAX; i++)
{
printf("%d \n",i);
}
return 0;
}

Output:

preprocessor in c1

Explanation: In the above program, we can see that we have defined macro with name as “MAX” which has value as 8. This means that the program takes a macro name in the program to print the numbers until the macro value defined in the beginning.

In C, macros are classified into two different types they are Object- like and function-like macros. In object-like macros are symbolic constants that are used to define identifiers.

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,570 ratings)
Course Price

View Course

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

For example #define PI 3.14

In function-like macros are expressions that are used to perform some particular operation.

#define SQUARE (s) s*s

Code:

#include<stdio.h>
#define SQUARE(s)s*s
int main()
{
printf("Welcome to Educba tutorials!\n\n");
int side = 3;
int area = SQUARE(side);
printf("The area is: %d\n", area);
return 0;
}

Output:

preprocessor in c2

Explanation: In the above program, we are defining the macro name “SQUARE” with an argument which is known as function-like macro and the above program uses the macro known as “MAX” where the value is assigned as 8 this type of the macro is known as an object-like macro.

2. Predefined macros in C

In the C programming language, ANSI C provides predefined macros that can be used in the programs. There is a list of predefined macros and are as follows:

  1. _DATE_ This macro defines the current date in the program and it will be displayed in the format “MMM DD YY”.
  2. _FILE_ this predefined macro gives the name of the current file that the program will display.
  3. _TIME_ this macro also defined the current time where it is displayed in the format as “HH: MM: SS”.
  4. _LINE_ this macro defines the current line number of the program.
  5. _STDC_ this macro has ANSI standard value as 1 when compiler compiles this ANSI standard.

Let us implement all the above-predefined macros in a single program to see how they display the output.

Code:

#include<stdio.h>
int main()
{
printf("Below are few predefined macros that are used in C:\n");
printf("This will print the current File name :%s\n", __FILE__ );
printf("This will print the current Date :%s\n", __DATE__ );
printf("This will print the current Time :%s\n", __TIME__ );
printf("This prints the current Line number :%d\n", __LINE__ );
printf("This prints the ANSI standard STDC :%d\n", __STDC__ );
return 0;
}

Output:

predefined macros

Explanation: In the above program, we have used all 5 predefined macros of ANSI C standard and we can see the different outputs.

Conclusion

In this article, we conclude that preprocessor in C programming language, is nothing but a small piece of code which is used as a single name that is defined at the beginning of the program known as macro and this macro can be used in the entire program any number of times whenever the value of the macro is needed to use you can simply specify the macro name in the program. There two types of macros they are object-like and function-like macros. There are also few predefined macros provided by the ANSI C standards.

Recommended Articles

This is a guide to Preprocessor in C. Here we discuss introduction to Preprocessor in C, how does it work , types with respective examples. You can also go through our other related articles to learn more –

  1. C++ Header Files
  2. Preprocessor Directives in C
  3. Constants in C
  4. Programming Errors in C

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

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 Course Learn More