strange value of int in C++

Hello friends!
I wrote a simple code and got output that I didn't understand the meaning of, somebody can explain it?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main() {
	string input;
	int output;
	cout << "debug - output value: " << output << endl;

	return 0;
}


Output of program

debug - output value: 22018


Why?
Last edited on
output is not initialized with a proper value, so it contains junk. For you, it happens to contain 22018.

More formerly: It is undefined behavior, and should be absolutely avoided.
https://en.cppreference.com/book/uninitialized

Note that this does not apply to your string, because it is a class with a default constructor that constructs it to be an empty string by default.
Last edited on
Thank You!
Topic archived. No new replies allowed.