const
variables in C++ must be initialized means uninitialized const variable
isn't possible & it is a compiler error. But why it is not same in C language also?
Consider following program that compiles fine C:
#include <stdio.h>
int main()
{
const int a;
}
What is the reason to allow uninitialized const
? Wouldn't it be nice If C also follows same rule as C++ does? Is it due to performance concerns that local const variable needs to be initialized every time when a function is called & initialization takes time?
A constant variable must be initialized at its declaration. To declare a constant variable in C++, the keyword const is written before the variable's data type. Constant variables can be declared for any data types, such as int , double , char , or string .
To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.
Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is given a memory address to use to store data, the default value of that variable is whatever (garbage) value happens to already be in that memory address!
A variable must be defined before it can be used. You cannot initialize a named constant that is declared with the const modifier.
The difference probably stems, among other things, from a significantly more relaxed approach to initialization in C language in general, not only with regard to const
objects. For example, this code is illegal in C++
goto over;
int a = 5;
over:;
because it jumps into scope of a
bypassing its initialization. Meanwhile in C this code is perfectly legal, with variable a
having indeterminate value at over:
.
The same logic applies to your const int a
declaration. C language simply believes that an uninitialized object is not a big deal, even in situations where it is no longer possible to set it to a determinate value later.
The primary reason for stricter initialization requirements in C++ is introduction of non-trivial initialization (constructors) into the language, i.e. initialization that cannot be meaningfully bypassed. Scalar objects and their initialization in C++ just tagged along as small part of a much broader concept.
Wouldn't it be nice If C also follows same rule as C++ does?
I don't see it. C and C++ are substantially different languages. And they treat const
quite differently as well.
History.
const
was specified in C++ from its beginning and the use met that language's goals. const
was later specified in C but with a related but different meaning to minimize exiting C code compatibility issues.
Since C began without const
, its later inclusion is more like a read-only
modifier than a constant
one. This allowed existing compilers to essential treat const
as nothing for writing to a const
is undefined behavior. Newer compilers/code could take advantage that const
provides.
const int a;
a = 5; // problem in C as code attempts to write `a`
// Really should be `const char *fred`, but allowed for backwards compatibility.
char *fred = "sally";
C++ took a stronger approach and demands the initialization.
See also const in C vs const in C++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With