Reputation: 37
I came across the following question:
#include<iostream>
using namespace std;
int main ()
{
int cin;
cin >> cin;
cout << "cin" << cin;
return 0;
}
The question asks me to find the output of the program, and the given options are:
(A) error in using cin keyword
(B) cin+junk value
(C) cin+input
(D) Runtime error
The answer is given to be as (B) cin+junk value, but no explanation has been given. Can someone please explain what is happening in this program?
Upvotes: 0
Views: 1080
Reputation: 234635
The question you've come across is absolutely evil, and the author has forgotten to include the correct answer.
The local cin
variable (which shadows std::cin
due to using namespace std;
) is uninitialized, and cin >> cin
is the binary bitwise shift-right operator on the uninitialized cin
variable.
This behaviour is not defined by the C++ standard. This means that the behaviour of the entire program is undefined.
So, this is no more than a trick question - these quirks can come up, and using a debugger will quickly identify them. Learn to code, don't learn how not to code. So very negative.
Upvotes: 4
Reputation: 148880
What happens here ?
#include<iostream> // ok: declares std::cin
using namespace std; // imports cin in main namespce
int main ()
{
int cin; // HIDES std::cin behind a local integer variable
// this is the integer shift operator,ON AN UNITIALIZED VARIABLE : UB
cin >> cin;
cout << "cin" << cin; // (could) dump junk because cin has never be initialized
return 0;
}
On most system, it will dump junk because it uses an unitialized variable. But Undefined Behaviour means that per standard absolutely anything could happen...
Upvotes: 3
Reputation: 117218
The only answer that it can not be is
(A) error in using cin keyword
They are probably looking for (B)
because the int
cin
is uninitialized - but reading it makes your program have undefined behavior so (B)
is not guaranteed.
int cin; // uninitialized
// UB: shifting a uninitialized variable by an uninitialized variable:
cin >> cin;
// UB: reading an uninitialized variable:
cout << "cin" << cin;
Since the program has UB It may not even print cin
. You may in fact get something totally unexpected, like
(C) cin+input // yes, even that - it'd be strange, but UB is UB
(D) Runtime error // and this
Upvotes: 1
Reputation: 27365
The answer is given to be as (B) cin+junk value, but no explanation has been given.
The answer is wrong: reading an uninitialized variable is undefined behavior.
While it is probable that you will get "cin" + junk value, that may last until you change compiler, you upgrade compiler, you change compilation optimization settings, you run your application while some other specific application is running, or (I guess) until planetary alignment changes :-\
The correct answer is "(possibly) none of the above" or "depends on compiler implementation" or "it is impossible to predict correctly".
Upvotes: 3
Reputation: 48258
is a good practice to :
respect the naming rules and dont "use namespaces std"
so just doing this
std::cin >> cin;
will work
Upvotes: 0