What do you think happens when you use an uninitialized primitive data type?
Well you may assume that the compiler should assign your primitive type variable with meaningful values like 0 for int, 0.0 for float. What about char data type?
Let’s find the answer to that by running the code in the IDE.
Output in GFGs IDE:
5.88052e-39 0 6.9529e-310 0
Output in Codechef IDE:
0 0 0 0
Output on my machine:
1.4013e-045 0 2.96439e-323 0
Why C/C++ compiler does not initialize variables with default values?
“One of the things that has kept C++ viable is the zero-overhead rule: What you don’t use, you don’t pay for.” -Stroustrup.
The overhead of initializing a stack variable is costly as it hampers the speed of execution, therefore these variables can contain indeterminate values. It is considered a best practice to initialize a primitive data type variable before using it in code.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Difference between fundamental data types and derived data types
- How to print range of basic data types without any library function and constant in C?
- What are the data types for which it is not possible to create an array?
- Data Types in C
- C | Data Types | Question 1
- C | Data Types | Question 2
- C | Data Types | Question 4
- C | Data Types | Question 5
- C | Data Types | Question 6
- C | Data Types | Question 7
- C | Data Types | Question 8
- C | Data Types | Question 9
- Interesting facts about data-types and modifiers in C/C++
- What happen when we exceed valid range of built-in data types in C++?
- Calculate range of data types using C++
- Linking Files having same variables with different data types in C
- User defined Data Types in C++
- Derived Data Types in C++
- C++ Data Types
- C++ default constructor | Built-in types
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.