For Programmers: Free Programming Magazines  


Home > Archive > C > June 2006 > Preprocessing









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Preprocessing
tatto0_2000@yahoo.com

2006-06-29, 6:56 pm

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.

Eric Sosman

2006-06-29, 6:56 pm



tatto0_2000@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.

--
Eric.Sosman@sun.com

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com