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"
......
if(BOOL==1)
......

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

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
#endif

#ifndef BOOL
   // do different stuff here
#endif

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.