Using Preprocessor Directives in C for Code Optimization


This program demonstrates the use of preprocessor directives in C programming language.

  • The program starts by using the "#define" preprocessor directive to create two constants: "LIMIT" and "MSG". The constant "LIMIT" is assigned the value of 5 and the constant "MSG" is assigned a string value "Tutor Joes".
  • Then, the program uses the "#define" preprocessor directive to create a custom macro called "custom_message" that takes an argument "a" and uses the printf function to display a message with the argument passed to the macro.
  • In the main function, the program uses a for loop to iterate from 0 to 4, and inside the loop, it uses the printf function to display the current value of the loop variable "i" which is a number between 0 and 4.
  • Next, the program uses the printf function to display the value of the constant "MSG" which is "Tutor Joes".
  • Then, the program uses the custom macro "custom_message" and passes the string "Ram Kumar" as an argument to it, which will display a message that says "Ram Kumar Welcome to our institution".
  • The program also uses three predefined macros FILE, TIME, LINE to print the name of the file, the current time, and the current line number respectively.
  • It's worth noting that the preprocessor directives start with a "#"

Source Code

//Preprocessor Directives  in C Programming
 
#include<stdio.h>
#define LIMIT 5
#define MSG "Tutor Joes"
 
#define custom_message(a)\
printf("\n" #a " Welcome to our institution")
 
int main()
{
 
    for(int i=0;i<LIMIT;i++)
    {
        printf("\n%d",i);
    }
 
    printf("\n%s",MSG);
    custom_message("Ram Kumar");
 
    printf("\nFile Name  : %s",__FILE__);
    printf("\nTime       : %s",__TIME__);
    printf("\nLINE       : %d",__LINE__);
 
    return 0;
}
 

Output

0
1
2
3
4
Tutor Joes
"Ram Kumar" Welcome to our institution
File Name  : C:\Users\tutor\Desktop\C_CLASS\01)Prepro.c
Time       : 15:24:48
LINE       : 21
To download raw file Click Here

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C