I am teaching myself C by going over my C++ book and recoding the problems in C. I wanted to know the correct industry standard way of declaring variables constant in C. Do you still use the #define directive outside of main, or can you use the C++ style const int inside of main?
-
3"constant variable" is an oxymoron :) – Greg Mattes Jun 30 '11 at 23:52
-
It may be an oxymoron, but that's exactly what the `const` keyword creates in C - not a *constant*, but rather a non-modifiable variable. – R.. GitHub STOP HELPING ICE Jul 01 '11 at 00:13
-
http://stackoverflow.com/questions/1944041/advantage-and-disadvantages-of-defines-vs-constants – jamesdlin Jul 01 '11 at 01:35
-
1The correct term for C is `const`-qualified object, that is this is not a property of the variable but of the object it refers to. – Jens Gustedt Jul 01 '11 at 05:51
5 Answers
const
in C is very different to const
in C++.
In C it means that the object won't be modified through that identifier:
int a = 42;
const int *b = &a;
*b = 12; /* invalid, the contents of `b` are const */
a = 12; /* ok, even though *b changed */
Also, unlike C++, const objects cannot be used, for instance, in switch labels:
const int k = 0;
switch (x) {
case k: break; /* invalid use of const object */
}
So ... it really depends on what you need.
Your options are
#define
: really const but uses the preprocessorconst
: not really constenum
: limited toint
larger example
#define CONST 42
const int konst = 42;
enum /*unnamed*/ { fixed = 42 };
printf("%d %d %d\n", CONST, konst, fixed);
/* &CONST makes no sense */
&konst; /* can be used */
/* &fixed makes no sense */

- 106,608
- 13
- 126
- 198
Modern C supports both #define
s and const
globals. However, #define
s are usually preferred for true constants; this is because #define
s can be inlined into the place where they are used, while const
globals generally require a memory read, particularly if they're defined in a different translation unit.
That said, complex constant structures are a good use for const
globals - strings, struct
s, arrays, etc.

- 224,562
- 31
- 268
- 324
-
1I don't at all agree that `#define`s are usually preferred to true constants. `enum` avoids the problems you mentioned and provides a symbolic name. Or rather: I don't agree that they usually *should* be preferred to true constants. – jamesdlin Jul 01 '11 at 01:36
-
2enums are limited to the range of an `int`, note. This is important when working on embedded platforms... – bdonlan Jul 01 '11 at 02:44
-
On modern systems `const`s are placed in the .rodata section which actually protects them – 0___________ Aug 15 '18 at 08:29
In most modern implementations the compiler is trying to do more just to bare the access via the symbol by placing the global const
variables in the read only sections. It actually protects them on many systems against change.
Some examples : segfault on Linux systems and errors on Windows or placing the code in the FLASH when using the micro controllers.
It is of course 100% implementation, but it is good to know that modern machines with memory protection hardware and modern compilers do more than only follow the standard

- 60,014
- 4
- 34
- 74
Variables qualified by const
are not considered compile-time constant, they have one significant limitation: const int
cannot be used to define the size of an array.(historical reasons but not a limitation of the compiler, C++ corrects this oversight though)
You can choose:
#define SIZE 5 /* preprocessor */
enum { SIZE=5 }; /* compiler */

- 2,958
- 2
- 13
- 32
The standard that is followed in most C programs is to have all constants as macros (#define) in a separate header file.

- 372
- 4
- 12