Working with Define in C

Working with Define in C programming by using the 'define' keyword. 'define' keyword is a preprocessor directive that allows you to define a macro, essentially a symbolic name.

Program Printing Values using Auto

Program

#include<stdio.h>
#include<conio.h>
void main()
{
auto int i = 1; //assigning value to auto 'i'.
{
auto int i = 2; //assigning value to another auto 'i'.
{
auto int i = 3; //assigning value to another auto 'i'.
printf("%d \n", i); //printing 'i' value'3'.
}
printf("%d \n", i); //printing 'i' value'2'.
}
printf("%d \n", i); //printing 'i' value'1'.
getch();
}
Output
3
2
1

Program Printing Values using Defined Value

Program

#include<stdio.h>
#include<conio.h>
#define bilal 12 //defining 'bilal' is 12.
void main()
{
int i;
for(i = 1; i <= bilal; i++) //loop looping from 0 to less then
printf("%d \n", i);          //Or equal to defined 'bilal' Value.
getch();
}
Output
1
2
3
4
5
6
7
8
9
10
11
12

Program Using Define

Program

#include<stdio.h>
#include<conio.h>
#define AND && //defining 'AND'.
#define ARANGE (a > 25 AND a < 50) //defining 'ARANGE'.
void main()
{
int a = 30;
if(ARANGE) //if 'ARANGE' mean if 'a' value greater then 25 And
{           //less then 50 then
printf("within range"); //print this.
}
else
{
printf("out of range");
}
getch();
}
Output
within range

Program Using Defined Lines
Program

#include<stdio.h>
#include<conio.h>
#define MyVal printf("This is First Line\n"); //defining.
#define MyVal2 printf("This is 2nd Line\n");
void main()
{
MyVal2 //using defined.
MyVal
getch();
}
Output
This is 2nd Line
This is First Line