Q:

The #if ... #else directive Example in C | C preprocessor programs

belongs to collection: C Preprocessors Programs

0

The #if ... #else directive Example in C | C preprocessor programs

The #if ... #else is a preprocessor directive in C programming language and it is used for conditional compilation, where one of the code section needs to be compiled based on the condition from two given code sections.

General for of the #if ... #else directive is:

    #if conditional_expression 
	    statements-true;
    #else
	    statements-false;
    #endif

If conditional_expression is true, the code written between in #if block i.e. statements-true will be executed and if conditional_expression is false, code written in #else block i.e. statements-false will be executed.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

#include <stdio.h>

#define MAX_GF 20

int main(){
	
	printf("Hello\n");
	#if MAX_GF>=50
		printf("Wau, you may have more than 50 girlfriends\n");
		printf("Code for more than 50 GFs\n");
	#else
		printf("Wau, you may not have more than 50 girlfriends\n");
		printf("Code for less than 50 GFs\n");		
	#endif
	printf("Bye!\n");
	
	return 0;
}

Output

    Hello
    Wau, you may not have more than 50 girlfriends
    Code for less than 50 GFs
    Bye!

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
The # Preprocessor Operator Example in C | C prepr... >>
<< The #if directive Example in C | C preprocessor pr...