0

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?

  • 2
    `cin` is not a keyword. It's a variable in the `std` namespace. Also see [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Jun 29 '21 at 08:00
  • what prevents you from compiling it to see what output you get? – 463035818_is_not_an_ai Jun 29 '21 at 08:01
  • what even "*cin+junk value*" try to say? – apple apple Jun 29 '21 at 08:02
  • As for the "junk value", remember that uninitialized local variables really are uninitialized, and will have an *indeterminate* value. Using such an indeterminate value in any way leads to *undefined behavior* and makes the whole program ill-formed. – Some programmer dude Jun 29 '21 at 08:02
  • @463035818_is_not_a_number I have compiled the program, and have got the answer, but I can't understand why I am getting the answer – newprogrammer Jun 29 '21 at 08:04
  • don't ignore compiler warnings, they have a lot to tell for this code: https://godbolt.org/z/8ssb5v3dh – 463035818_is_not_an_ai Jun 29 '21 at 08:05
  • @Someprogrammerdude I agree with you that we shouldn't use namespace std; but this is the question as was asked to me, I obviously can't ask the examiner to stop using namespace std and use std::cout, it would be better if you could explain why this program outputs cin+garbage value. – newprogrammer Jun 29 '21 at 08:05
  • As a final hint: The `>>` operator you use isn't the "input" operator, it's the bitwise right shift operator. – Some programmer dude Jun 29 '21 at 08:05
  • Nothing wrong with asking a question about a terrible question. And this is clear, and the answer obvious. Have an upvote! – Bathsheba Jun 29 '21 at 08:10

5 Answers5

4

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • It's actually there on GeeksForGeeks : https://www.geeksforgeeks.org/c-misc-c-question-8/. Stop Criticising me, I haven't designed this question, I just came across this question and asked it. Please be humble and modest in life, especially with beginners in coding. – newprogrammer Jun 29 '21 at 08:08
  • 2
    @newprogrammer: Dude, I'm not criticising you at all - but I would like to have a conversation with the author of the examination. – Bathsheba Jun 29 '21 at 08:09
  • 2
    @newprogrammer It's not your question that's "evil", but rather the original question you're asking about. It's really a trick question where none of the alternatives are correct, though I don't think the original writer of the question realize that. – Some programmer dude Jun 29 '21 at 08:13
3

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...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • "What happens here ?" -> "What might happen here ?". It's not entirely sensible to think of code which has UB in its only control path as doing anything predictable. I wonder if I can find a mainstream compiler that optimises the code to `int main(){}` with optimisations cranked up? Or specifically, `cout << "cin" << cin` could trap. – Bathsheba Jun 29 '21 at 08:21
  • 1
    @Bathsheba: You are right. I wrote UB on previous line, and yet let think on next line that behaviour could be deterministic. Hope it is more clear now... – Serge Ballesta Jun 29 '21 at 08:52
2

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".

utnapistim
  • 26,809
  • 3
  • 46
  • 82
1

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
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

is a good practice to :

respect the naming rules and dont "use namespaces std"

so just doing this

std::cin >> cin;

will work

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97