
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Uninitialized Primitive Data Types in C/C++
In this section we will see when we declare one variable that is un-initialized, which value they hold in C or C++ language. Sometimes we assume that the compiler assigns some value like 0 for int, 0.0 for float etc. But what will be for character datatype? Let us see using implementation and compile using different compilers.
Example (C++)
Let us see the following implementation to get better understanding −
#include <iostream> using namespace std; int main() { char char_var; float float_var; int int_var; double double_var; long long_var; cout << char_var << endl; cout << float_var << endl; cout << int_var << endl; cout << double_var << endl; cout << long_var << endl; }
Output (Compiled in Cpp.sh)
0 0 0 0
Output (Compiled in Online GDB)
5.88054e-39 0 6.95297e-310 0
Output (Local System)
9.73438e-039 4309744 1.15685e-306 -53505136
Now the question comes why C/C++ compiler does not initialize variables with default values? The answer is simple, the overhead of initializing a stack variable is expensive as it hampers the speed of execution, as a result of this these variables can consist of indeterminate values. So, it is treated as good practice to initialize a primitive data type variable before applying it in code.