Hi guys, I have a simple noob question for you.
say we got a header file called test.h and it contains the following content: #define BOOL and in my c file test.c I want to see whether BOOL is defined, can I use the if statement to test it like this:

#include "test.h"

Is it assumed that once a macro is defined, it is equal to 1 in a C program?
THANX in advance guys!

8 Years
Discussion Span
Last Post by jephthah

no you can't do that. because the BOOL could be defined as value zero. or seventyelevenbillion.

or, worse yet, BOOL could not be defined at all. then what would happen to your conditional evaluation???

so, i believe the thing you're looking for is the preprocessor directive, "#ifdef"... use it like so:

#ifdef BOOL
   // do stuff here

#ifndef BOOL
   // do different stuff here

the code that you write between the #ifdef and #endif (or #ifndef and #endif) will only be compiled if the statement evaluates as TRUE. otherwise it won't ever be compiled, much less run.


Is it assumed that once a macro is defined, it is equal to 1 in a C program

no, it is not. normally you always specify a value to the identifier you want to #define .

if you dont specify a value, it is the preprocessor will replace the identifiers with blank space or an empty string... not a "1".

the only time you should not specify a value, is when you're using the identifier to enable conditional compilation like i described with the #ifdef / #endif pairs.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.