#ifndef
The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code otherwise #else code is executed, if present. Syntax:
#ifndef MACRO //code #endif
Syntax with #else:
#ifndef MACRO//successful code #else//else code #endif
#ifndef example
see a simple example to use #ifndef preprocessor directive.#include <stdio.h> #define INPUT void main() { int b=0; #ifndef INPUT b=4; #else printf("Enter b:"); scanf("%d", &b); #endif printf("Value of b: %d\n", b); getch(); } Output: Enter b:10 Value of b: 10
#include <stdio.h> void main() { int b=0; #ifndef INPUT b=4; #else printf("Enter b:"); scanf("%d", &b); #endif printf("Value of b: %d\n", b); } Output: Value of b: 4
#if
The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed. Syntax:
#if expression//code #endif
Syntax with #else:
#if expression//if code #else//else code #endif
Syntax with #elif and #else:
#if expression//if code #elif expression//elif code #else//else code #endif
#if example
see a simple example to use #if preprocessor directive.#include <stdio.h> #define NUMBER 10 void main() { #if (NUMBER==10) printf("Value of Number is: %d",NUMBER); #endif } Output: Value of Number is: 10
#include <stdio.h> #define NUMBER 5 void main() { #if (NUMBER==1) printf("2 Value of Number is: %d",NUMBER); #endif #if (NUMBER==5) printf("3 Value of Number is: %d",NUMBER); #endif } Output: 3 Value of Number is: 5