How to check whether a Macro is defined or not in C?

Here, we will learn how to check whether a Macro is defined or not in C? To check – A Macro is defined or not, we use #ifdef preprocessor directive.
By IncludeHelp Last updated : March 10, 2024

To check whether a Macro is defined or not in C language – we use #ifdef preprocessor directive, it is used to check Macros only.

Syntax

#ifdef MACRO_NAME
	//body
#endif 

If MACRO_NAME is defined, then the compiler will compile //body (a set of statements written within the #ifdef ... #endif block).

Example

#include <stdio.h>

#define NUM 100

int main()
{
	//checking a defined Macro 
	#ifdef NUM
		printf("Macro NUM is defined, and its value is %d\n",NUM);
	#else
		printf("Macro NUM is not defined\n");
	#endif 

	//checking an undefined Macro 
	#ifdef MAX
		printf("Macro MAX is defined, and its value is %d\n",MAX);
	#else
		printf("Macro MAX is not defined\n");
	#endif     

	return 0;
}

Output

Macro NUM is defined, and its value is 100
Macro MAX is not defined

C Preprocessors Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.