By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Preprocessing

P: n/a
Hi,

I am a bit confuse. If I have the header file hdr.h with the
following:

#ifndef HDR_H
#define HDR_H
#define a 10
#define b 20
#endif

In file1.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

In file2.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

If these 2 C files to be compiled in the same project, would it still
legal? Can I get access to both constants in both files?

Thanks.

Jun 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a


ta*********@yahoo.com wrote On 06/29/06 12:35,:
Hi,

I am a bit confuse. If I have the header file hdr.h with the
following:

#ifndef HDR_H
#define HDR_H
#define a 10
#define b 20
#endif

In file1.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

In file2.c:
#include "hdr.h"
/* and other codes to access constant a and b after this */

If these 2 C files to be compiled in the same project, would it still
legal? Can I get access to both constants in both files?


Yes and yes. However, I think you do not understand
the nature of a and b. They are not "constants," but
"macros." After the #defines, each appearance of a or b
is recognized as a reference to a macro, and the reference
is replaced by the macro's definition. In this case, the
definitions are numeric constants, but other macros could
be defined differently. HDR_H, for example, is a macro
whose definition is empty: if you were to use HDR_H in the
code, the compiler would recognize it as a macro and replace
it with its definition, namely, nothing at all.

You need to re-read your C textbook until you understand
the difference between a macro and a variable.

--
Er*********@sun.com

Jun 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.