new to c #define

A

ampeloso

Hello,
I am modifying some code and there is a #define with no value. It looks
like its used for testing.There are several #ifdef references.Is this
ignored if there is no value assigned?
Thanks
Mike
 
W

Walter Roberson

I am modifying some code and there is a #define with no value. It looks
like its used for testing.There are several #ifdef references.Is this
ignored if there is no value assigned?

No, a #define with no value defines the value as 0 but marks it as
defined.

Be careful using the variable in preprocessor expressions:

#define FOO
#define BAR 1
/* BAZ is not defined at all */

then taking each of the below individually:


#ifdef FOO /* this is true */

#ifdef BAR /* this is true */

#ifdef BAZ /* this is false */

#if FOO /* this is false because FOO's value 0 is substituted */

#if BAR /* this is true because BAR's value 1 is substituted */

#if BAZ /* this is false because all symbols remaining after
preprocessing have 0 substituted for them, so
0 is put in in place of BAZ */

#if defined(FOO) /* this is true */

#if defined(BAR) /* this is true */

#if defined(BAZ) /* this is false */

#if !FOO /* this is true because FOO is 0 and !0 is true */

#if !BAR /* this is false because BAR is 1 and !1 is false */

#if !BAZ /* this is true because remaining symbols have 0
substituted for them, making this !0 which is true */
 
E

Eric Sosman

Hello,
I am modifying some code and there is a #define with no value. It looks
like its used for testing.There are several #ifdef references.Is this
ignored if there is no value assigned?

The macro is defined, but with an empty value. If
you use the macro somewhere, it will be replaced by its
expansion, which is empty -- in effect, the macro just
disappears.

However, even though the macro's value is empty, the
macro is defined. Since #ifdef and #if defined ... and
#ifndef test the defined/undefined status and not the
value, they will all "see" the macro.
 
C

CBFalconer

I am modifying some code and there is a #define with no value. It
looks like its used for testing.There are several #ifdef references.Is
this ignored if there is no value assigned?

What kind of a file or record field is "testing.There", and
"references.Is"?
 
K

Keith Thompson

No, a #define with no value defines the value as 0 but marks it as
defined.

No, it expands to an empty sequence of tokens.

Given "#define FOO", the directive "#if FOO" is illegal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top