Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why const variable need not to be initialized in C?

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?

like image 459
Destructor Avatar asked Jun 23 '15 13:06

Destructor


People also ask

Do const variables need to be initialized?

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 .

Can const be initialized?

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.

What happens if I don't initialize a variable in C?

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!

Can you initialize a named constant?

A variable must be defined before it can be used. You cannot initialize a named constant that is declared with the const modifier.


2 Answers

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.

like image 93
AnT Avatar answered Oct 14 '22 20:10

AnT


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

like image 27
chux - Reinstate Monica Avatar answered Oct 14 '22 18:10

chux - Reinstate Monica