Q:

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

belongs to collection: C Preprocessors Programs

0

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

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).

 

All Answers

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

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

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
Print argument (i.e. variable name, value) using M... >>
<< How to redefine a Macro in C?...