q for the C hackers

Kevin D. Clark kclark at CetaceanNetworks.com
Tue Aug 19 09:07:23 EDT 2003


Aaron Hope <Aaron.Hope at unh.edu> writes:

> On Mon, 2003-08-18 at 20:13, 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;
> 
> Generally, the more the compiler knows, the better it can do it's job,
> so I usually prefer the latter.  There are still some places in C (C99
> specifically) where you you have no choice but to use macros, like array
> declarations.  

Another possibility is to use enums, i.e.:


        enum { BUFSIZE=512 };

        char arr[BUFSIZE];

I use this frequently, and I recommend this.

> I believe that C++ const variables can be used wherever a
> simple #define can.

Well, they can be used for array initialization too.

--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