q for the C hackers

Kevin D. Clark kclark at CetaceanNetworks.com
Tue Aug 19 08:26:22 EDT 2003


Derek Martin <invalid at pizzashack.org> writes:

> On Mon, Aug 18, 2003 at 08:13:05PM -0400, Erik Price wrote:
> > However, what is the convention in C?  There seem to be two fine
> > ways of doing it -- using the preprocessor, or the const keyword:
> > 
> > #define NUMBER_OF_UNITS 8
> > 
> > const int NUMBER_OF_UNITS = 8;
> 
> Normally, the former way is what you'd use.  The latter construct is
> most often used, in my experience, for function parameters which you
> want to make sure are not modified by the function to which they
> belong.  

This is confused.  Erik wasn't even asking about function parameters.
He just wants to use a constant in his program.

Sure, "const" carries some meaning when used with function parameters,
but Erik wasn't asking about this.

> I'm not a compiler guru, but I believe references to the #define'd
> macro will cause the compiler to use immediate mode addressing, using
> the value of the macro literally, requiring no additional storage
> beyond what is required to store the opcode and its operands.  Whereas
> the second method allocates storage in the segment of memory
> associated with initialized data that hangs around, so that it can
> contain the constant.

Like you stated in a later post, exactly *what* the compiler does for
these cases is implementation defined.

--kevin
-- 
Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA)
cetaceannetworks.com!kclark (GnuPG ID: B280F24E)
alumni.unh.edu!kdc




More information about the gnhlug-discuss mailing list