Looking into languages such as Java & C# use of uninitialized local variable is compile time error. Then why C & C++ allows uninitialized local variables? What is the reason that these languages allows this? I think many of the bad problems can't arise or can be prevented if these 2 languages forces programmer to mandatory initialize local variables including pointers & it also makes language more secure. Doesn't it?
-
6The idea is that you don't pay for what you don't need. You don't always need initialization, so you shouldn't always have to pay for it. – juanchopanza Feb 16 '15 at 17:09
-
@meet, it is a tradeoff of efficiency for safety. – merlin2011 Feb 16 '15 at 17:11
-
3Yep, those can be big problems. You're supposed to know what you're doing and make no errors. I know, it is a huge assumption to make. – juanchopanza Feb 16 '15 at 17:11
-
2C is a very old language (1972), offering significant improvements over its predecessor language B with features like "type checking". Back in those days, programs were limited to 64KB in size, so keeping things small was of critical importance. – Raymond Chen Feb 16 '15 at 17:12
4 Answers
The C language is well known for producing very fast and efficient code.
With that in mind, it makes sense for the language not to automatically initialize all variables. With languages that automatically initialize variables, if you later initialize them in code, they actually get initialized twice, which is less efficient and serves no purpose.
You are correct, C is an advanced language and it requires more care by experienced developers to ensure problems are not introduced by forgetting to initialize variables, or forgetting to do other things that are not automatic.

- 65,341
- 71
- 269
- 466
int main() {
int i;
// Here i is uninitialized
scanf("%d", &i);
}
You don't need i
to be initialized before scanf()
. For such cases, C doesn't waste cycles initializing everything.

- 62,093
- 7
- 131
- 191
-
1Yes. Similarly with large disk/network buffers - initializing them just before reading into them is just silly. – Martin James Feb 16 '15 at 17:22
-
1@MartinJames yes, I used an `int`, but it would be the same (and more concerning) for `char[BIG]`. – Quentin Feb 16 '15 at 17:23
C is designed to be a tool to allow great programmers to write efficient code, rather than to prevent beginners from shooting themselves in the foot. Allowing the use of uninitialized variables is a nod in that direction.
Here is a good description from this article which discusses the advantages of undefined behavior in general.
Use of an uninitialized variable: This is commonly known as source of problems in C programs and there are many tools to catch these: from compiler warnings to static and dynamic analyzers. This improves performance by not requiring that all variables be zero initialized when they come into scope (as Java does). For most scalar variables, this would cause little overhead, but stack arrays and malloc'd memory would incur a memset of the storage, which could be quite costly, particularly since the storage is usually completely overwritten.

- 71,677
- 44
- 195
- 329
C++ allows them because C does. Where possible, C++ compilers accept valid C programs as input.
As for C: in olden times, memory was scarce and operations were slow. Initializing a variable that did not yet need initialization was wasted time and space.
Since all variables had to be declared at the start of a function definition (often before the value that would initialize them was known), uninitialized variables were a necessity. Dangerous, but efficient. There are a lot of trade-offs in the world.

- 36,322
- 27
- 84
- 93