J
James Harris
I'm a bit confused over how 'best' to define constants - especially small
integer constants - in C. There seem to be some options so I wondred if you
guys would recommend one over the others.
I'll use a 16-bit port number in each example. The ui16 in the last example
is the typedef of a 16-bit unsigned int.
First option, with #define.
/* Define address port */
#define PORT_A 0x70
Second option, using enum
enum {
port_a = 0x70 /* Address port */
};
Third option, using const
static const ui16 port_a = 0x0070; /* Address port */
Are they all valid and are there any other ways to define integer constants?
I think I know the basics (#define is preprocessor, textual, works for more
than just numbers, widely used, needs parens if an expression etc; enum is
good for assigning consecutive integers, includes limited type info,
possibly modifiable through a pointer(?); const not guaranteed to be safe as
can be modified through a pointer but includes good type info).
Assuming they are all valid, under what circumstances would you choose one
over the others? If you had to use #define for some constants such as string
literals would you use #define for the integer constants too for
consistency?
Would you ever define an enum tag and/or define a variable using the enum?
I know the static keyword would keep the id private to the source file but
would the const be better with or without static? Would that depend on
whether the definition was in the .c source file or was in a header file
that might be included in multiple object files?
Finally, what would you recommend wrt capitalisation of the identifier name?
As you can see, I've used both lower case and upper case in the different
examples. Aside from the #define I'm not sure what's considered to be more
normal.
Sorry that's a lot of questions!
James
integer constants - in C. There seem to be some options so I wondred if you
guys would recommend one over the others.
I'll use a 16-bit port number in each example. The ui16 in the last example
is the typedef of a 16-bit unsigned int.
First option, with #define.
/* Define address port */
#define PORT_A 0x70
Second option, using enum
enum {
port_a = 0x70 /* Address port */
};
Third option, using const
static const ui16 port_a = 0x0070; /* Address port */
Are they all valid and are there any other ways to define integer constants?
I think I know the basics (#define is preprocessor, textual, works for more
than just numbers, widely used, needs parens if an expression etc; enum is
good for assigning consecutive integers, includes limited type info,
possibly modifiable through a pointer(?); const not guaranteed to be safe as
can be modified through a pointer but includes good type info).
Assuming they are all valid, under what circumstances would you choose one
over the others? If you had to use #define for some constants such as string
literals would you use #define for the integer constants too for
consistency?
Would you ever define an enum tag and/or define a variable using the enum?
I know the static keyword would keep the id private to the source file but
would the const be better with or without static? Would that depend on
whether the definition was in the .c source file or was in a header file
that might be included in multiple object files?
Finally, what would you recommend wrt capitalisation of the identifier name?
As you can see, I've used both lower case and upper case in the different
examples. Aside from the #define I'm not sure what's considered to be more
normal.
Sorry that's a lot of questions!
James