C #ifdef
Example
#ifdef MACRO
//code
#endif
Syntax with #else:
Example
#ifdef MACRO
//successful code
#else
//else code
#endif
C #ifdef example
Let's explore a basic illustration demonstrating the utilization of the #ifdef preprocessor directive.
Example
Example
#include <stdio.h>
#include <conio.h>
#define NOINPUT
void main() {
int a=0;
#ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}
Output:
Output
Value of a: 2
However, failing to specify NOINPUT will result in a prompt for the user to input a numerical value.
Example
Example
#include <stdio.h>
#include <conio.h>
void main() {
int a=0;
#ifdef NOINPUT
a=2;
#else
printf("Enter a:");
scanf("%d", &a);
#endif
printf("Value of a: %d\n", a);
getch();
}
Output:
Output
Enter a:5
Value of a: 5