what will be the value of #define

R

raghu

Hello
I have a doubt plz clarify that
#ifndef SYSTEM_H
#define SYSTEM_H
what will be the value of SYATEM_H after this #define statement and
before that statement.

Thanking you all

Bye
 
R

Richard Heathfield

raghu said:
Hello
I have a doubt plz clarify that
#ifndef SYSTEM_H
#define SYSTEM_H
what will be the value of SYATEM_H after this #define statement and
before that statement.

Your #define doesn't affect any identifier by the name of SYATEM_H -
but, presuming you meant SYSTEM_H, its value prior to the #ifndef
depends on whether it exists. If it didn't, then its value after the
#define (in your example) will be 0.
 
E

Eric Sosman

raghu said:
Hello
I have a doubt plz clarify that
#ifndef SYSTEM_H
#define SYSTEM_H
what will be the value of SYATEM_H after this #define statement and
before that statement.

(Just in case it's a trick question): Impossible to tell,
based on the information provided.

(Assuming it's just a typo): Before the #define, SYSTEM_H
is not defined and has no value at all. After it, SYSTEM_H is
defined as an object-like macro whose replacement list is empty.
 
M

mark_bluemel

(Just in case it's a trick question): Impossible to tell,
based on the information provided.

(Assuming it's just a typo): Before the #define, SYSTEM_H
is not defined and has no value at all. After it, SYSTEM_H is
defined as an object-like macro whose replacement list is empty.

To explain a little more for "raghu" - the approach is a standard
convention for ensuring that multiple inclusions of header files
(usually due to being nested in other header files) are "harmless".

If my header called "fred.h" has the form:-

#ifndef FRED_H
#define FRED_H
....
#endif /*FRED_H*/

then the first inclusion will have the macro "FRED_H" undefined, will
define the macro (as an empty macro) and do the other declarations,
definitions etc which the header defines.

Any subsequent inclusions will have "FRED_H" defined, so will do
nothing.
 
R

Richard Heathfield

Eric Sosman said:
(Just in case it's a trick question): Impossible to tell,
based on the information provided.

(Assuming it's just a typo): Before the #define, SYSTEM_H
is not defined and has no value at all. After it, SYSTEM_H is
defined as an object-like macro whose replacement list is empty.

Ah, good point. I realise now that my parallel reply may seem a little
odd, but of course I was thinking of its value when used in
preprocessor directives such as #if.
 
R

raghu

To explain a little more for "raghu" - the approach is a standard
convention for ensuring that multiple inclusions of header files
(usually due to being nested in other header files) are "harmless".

If my header called "fred.h" has the form:-

#ifndef FRED_H
#define FRED_H
...
#endif /*FRED_H*/

then the first inclusion will have the macro "FRED_H" undefined, will
define the macro (as an empty macro) and do the other declarations,
definitions etc which the header defines.

Any subsequent inclusions will have "FRED_H" defined, so will do
nothing.

Yes you are correct. I am imlementing exactly like this but one thing
I didn't understand what will be the value assigned FRED_H here. when
the processor looks in to it their must be change in the value. Right.
what will be that value.

Thanks for ur reply.

Bye
 
S

santosh

raghu said:
Yes you are correct. I am imlementing exactly like this but one thing
I didn't understand what will be the value assigned FRED_H here. when
the processor looks in to it their must be change in the value. Right.
what will be that value.

Thanks for ur reply.

No particular value will be assigned. The symbol FRED_H is defined and
will remain defined until explicitly undefined with an #undef
directive. If you want, you can also do:

#ifndef FRED_H
#define FRED_H 1

/* ... */

#endif /* FRED_H */
 
K

Keith Thompson

raghu said:
raghu wrote:
Hello
I have a doubt plz clarify that
#ifndef SYSTEM_H
#define SYSTEM_H
what will be the value of SYATEM_H after this #define statement and
before that statement. [...]
(Assuming it's just a typo): Before the #define, SYSTEM_H
is not defined and has no value at all. After it, SYSTEM_H is
defined as an object-like macro whose replacement list is empty.

To explain a little more for "raghu" - the approach is a standard
convention for ensuring that multiple inclusions of header files
(usually due to being nested in other header files) are "harmless".

If my header called "fred.h" has the form:-

#ifndef FRED_H
#define FRED_H
...
#endif /*FRED_H*/

then the first inclusion will have the macro "FRED_H" undefined, will
define the macro (as an empty macro) and do the other declarations,
definitions etc which the header defines.

Any subsequent inclusions will have "FRED_H" defined, so will do
nothing.

Yes you are correct. I am imlementing exactly like this but one thing
I didn't understand what will be the value assigned FRED_H here. when
the processor looks in to it their must be change in the value. Right.
what will be that value.

Thanks for ur reply.

Raghu, you've posted here a number of times. You've probably already
been advised not to use silly abbreviations like "plz" and "ur". They
just make what you write more difficult to read. If you want us to
take the time to read your articles, take the time to spell out the
words.

Given

#define SYSTEM_H

the identifier SYSTEM_H is a macro that expands to nothing. Any uses
of it *outside a preprocessing directive* (such as #if or #ifdef) will
simply vanish. For example, this:
SYSTEM_H int x SYSTEM_H = 42 SYSTEM_H ;
is exactly equivalent to this:
int x = 42 ;

But there's no reason to care what it expands to, because that's not
what it's used for. In the common idiom where a macro is used as a
header guard, the macro name is *only* used for the purpose of
determining whether it's defined or not, using "#ifndef"; what it
expands to is irrelevant.
 
S

santosh

Given

#define SYSTEM_H

the identifier SYSTEM_H is a macro that expands to nothing. Any uses
of it *outside a preprocessing directive* (such as #if or #ifdef) will
simply vanish. For example, this:
SYSTEM_H int x SYSTEM_H = 42 SYSTEM_H ;
is exactly equivalent to this:
int x = 42 ;

A minor question:

Is it simply removed or is it replaced by a space character?

<snip>
 
R

Richard Heathfield

Keith Thompson said:

Raghu, you've posted here a number of times. You've probably already
been advised not to use silly abbreviations like "plz" and "ur". They
just make what you write more difficult to read. If you want us to
take the time to read your articles, take the time to spell out the
words.

I have already stopped bothering to read his articles, for precisely
this reason. When he starts posting intelligibly, I will consider
reading what he has to say.
 
R

Richard Heathfield

Richard Heathfield said:
Keith Thompson said:



I have already stopped bothering to read his articles, for precisely
this reason. When he starts posting intelligibly, I will consider
reading what he has to say.

What a fib! A minimal amount of research shows that I do in fact still
read his articles, and even reply to them!

Never mind - I can soon put that right.
 
E

Eric Sosman

santosh said:
A minor question:

Is it simply removed or is it replaced by a space character?

The macro is removed. That's what happens when any macro
is expanded: the macro itself is removed, and its expansion is
inserted where the macro used to be.

The replacement for this macro is not a space character,
but a zero-length sequence of preprocessing tokens. A "raw"
character of any kind would be out of place here, since the
program text has by this time been tokenized; translation is
now dealing with (preprocessing) tokens, not with characters.
 
D

David Thompson

Eric Sosman said:


Ah, good point. I realise now that my parallel reply may seem a little
odd, but of course I was thinking of its value when used in
preprocessor directives such as #if.

I hope not, because that would have been wrong. You may have been, and
perhaps were, thinking of the value in a preprocessor expression of an
identifier (well, pp-identifier) which has NOT been #define'd at all.

The usual trick(?) to use a possibly-empty macro in a pp-expression is
#if FOO + 0 /* but not 0 + FOO ! */
/* selected if FOO is 1, unselected if FOO is 0 or empty */
#endif

and #if FOO + 0 >= 3 /* instead of just FOO >= 3 */ etc.
 
R

Richard Heathfield

David Thompson said:
I was thinking of [the] value [of an 'empty' #define] when used in
preprocessor directives such as #if.

I hope not, because that would have been wrong.

I was. If it is convenient, please convince me that I'm wrong. (If not,
don't worry - I'll simply add a note to my to-do list that I should try
to convince myself that I'm wrong, provided of course that I can still
find the end of the list.)
 
G

Guest

Richard said:
David Thompson said:
I was thinking of [the] value [of an 'empty' #define] when used in
preprocessor directives such as #if.

I hope not, because that would have been wrong.

I was. If it is convenient, please convince me that I'm wrong.

#undef MACRO
#define MACRO /* empty */
#if MACRO
#endif

Since MACRO is defined on the third line, it is expanded as usual. The
result is a line consisting only of #if, which is a syntax error.
There is a special replacement of identifiers with 0, but this is only
after the usual macro replacement, so by the time this is done,
"MACRO" is already gone.
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top