Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C allows uninitialized local variables?

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?

like image 286
Destructor Avatar asked Dec 19 '22 06:12

Destructor


1 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.

like image 52
Jonathan Wood Avatar answered Jan 09 '23 01:01

Jonathan Wood