The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present.
Syntax:
#ifdef MACRO //code #endif
Syntax with #else:
#ifdef MACRO //successful code #else //else code #endif
Let's see a simple example to use #ifdef preprocessor directive.
#include#include #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:
But, if you don't define NOINPUT, it will ask user to enter a number.
#include#include 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: