Home  >  Article  >  Backend Development  >  Uninitialized primitive data types in C/C++

Uninitialized primitive data types in C/C++

WBOY
WBOYforward
2023-08-28 22:17:03976browse

Uninitialized primitive data types in C/C++

One of the most common questions is what will the uninitialized raw data value be in C or C++? Well, the answer is different in different systems. We can assume that the compiler will assign the variable a value of 0. For integers, you can assign it a value of 0, for floating point numbers, you can assign it a value of 0.0, but for character type data, what will it be?

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 comes the question , why doesn't C or C++ assign some default value to the variable? The answer is that initializing stack variables is expensive. It also affects execution speed. Therefore, these variables may contain some intermediate values. So we need to initialize the value of primitive data type before using it.

The above is the detailed content of Uninitialized primitive data types in C/C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete