Pages

Friday, December 31, 2021

Use of #define in C program

Use of #define 

In the C Programming Language, the #define directive allows the definition of macros within your source code.

Macros are a portion of the code which is given some name. Whenever this name is encountered by the compiler the compiler replaces the name with the actual piece of code.

In the following example, we have used

#define DISPLAY printf

During execution all the DISPLAY word will be replaced with printf first.

Example of #define in C program


#include<stdio.h>
#include<Windows.h>
#define DISPLAY printf

int main()
{
    int i;
    for(i=0; i<3; i++)
    {
      DISPLAY("..... Good Bye 2021 .....");
      Sleep(500);
      system("cls");
      Sleep(500);
    }
    
    for(i=0; i<3; i++)
    {
      DISPLAY("..... WEL COME 2022 .....");
      Sleep(500);
      system("cls");
      Sleep(500);
    }
    return 0;
}

Output of the program

This will blinks three times

 ..... Good Bye 2021 ..... 

and then blinks three times 

..... WEL COME 2022 .....