Same input but different ways of output different results?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Same input but different ways of output different results?


#include <iostream>

using namespace std;

int main()

int a, b;

cin >> a >> b;

//First type of output
//cout << a << endl << b << endl;


//Second type of output
cout << "A: " << a << endl << "B: " << b << endl;

return 0;



Just a simple input and output, for the input if I input 'a' only, the first type will output
0
6422368



while with the same input, the second output will output
A: 0
B: -2



Anyone can explain why?





I think that's a residual value. When you don't set any value to an integer, and you just declare it, that variable has value. (I think that's the problem, I can't remember very well from college). So, if you'd like to have a default value (e.g.: 0), int a = 0, b = 0;
– GasKa
Aug 13 at 5:37



int a = 0, b = 0;





Undefined behavior. If you've not initialized your variables, reading them is undefined behavior. Your code could do anything. Always check for errors on I/O functions.
– Mat
Aug 13 at 5:38





Entering the character a causes reading to cease, and leaves the character 'a' waiting to be read on subsequent operations. This leaves the variable being read (and subsequent variables being read) unchanged. In your code, both a and b are uninitialised, so accessing their values gives undefined behaviour. Since the input operations have failed, the output statement therefore give undefined behaviour i.e. anything can happen.
– Peter
Aug 13 at 5:43


a


'a'


a


b




2 Answers
2



This is what is called as an undefined behavior.



When you input a character 'a' where actually an integer was expected. This character 'a' remains on the stream to be read by any further input operation. And at the same time both variables int a, b; remained un-initialized and using an un-initialized variables always leads to an undefined behavior. The output you are getting is an example of undefined behavior.


int a, b;



From the online cpp reference on Uninitialized variables:



It is possible to create a variable without a value. This is very dangerous, but it can give an efficiency boost in certain situations.
To create a variable without an initial value, simply don’t include an initial value:


// This creates an uninitialized int
int i;



The value in an uninitialized variable can be anything – it is unpredictable, and may be different every time the program is run. Reading the value of an uninitialized variable is undefined behaviour – which is always a bad idea. It has to be initialized with a value before you can use it.


#include <iostream>
int main()

int i; // uninitialized variable
 
// WARNING: This causes undefined behaviour!
std::cout << i << 'n';
 
i = 23; // initializing the variable
 
// Now it is safe to use.
std::cout << i << 'n';
 
return 0;



The value printed on the first line in the example above can be anything.





What about the cin >> a >> b; statement?! Wouldn't that define a and b?
– Alexis Wilke
Aug 13 at 5:58



cin >> a >> b;





@AlexisWilke: He is inputting the value for one of the variables only.
– P.W
Aug 13 at 6:03





@user846834 quoting from an other source if fine, and you also mentioned which one it is. But you should also link that, it is not clear for everyone that you mean cppreference.com with online cpp reference.
– t.niese
Aug 13 at 6:14



cppreference.com


online cpp reference





@t.niese: Okay, will do that from now onwards. Thanks for editing and adding the link.
– P.W
Aug 13 at 6:15






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard