Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Nishtha Thakur

One of the most frequent question is what will be the value of some uninitialized primitive data values in C or C++? Well the answer will be different in different systems. We can assume the compiler will assign 0 into the variables. It can be done for integer as 0, for float 0.0, but what will be for character type data?

Example

#include <iostream>
using namespace std;
main() {
   char a;
   float b;
   int c;
   double d;
   long e;
   cout << a << "\n";
   cout << b << "\n";
   cout << c << "\n";
   cout << d << "\n";
   cout << e << "\n";
}

Output (On Windows Compiler)

1.4013e-045
0
2.91499e-322
0

Output (On Linux Compiler)

0
0
0
0

So, now the question comes, why C or C++ is not assigning some default value for variables? The answer is, the overhead of initializing stack variables is costly. It hampers the speed of execution also. So these variables may contain some intermediate value. So we need to initialize the primitive datatype values before using it.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.