-7
int a,b,c; 
a=b++, c++; //error here

I'm trying to test if a will get the value of c++

Error Message:

Uninitialized local variable 'b' used

Uninitialized local variable 'c' used

EDIT: I read this on the internet http://www.studytonight.com/cpp/operators-and-their-types.php, but it is wrong that's what other user's say

  • 5
    "I'm trying to test if a will get the value of c++" so... what's the value of `c++`? – George Mar 15 '17 at 13:59
  • 3
    maybe initialize `b` and `c`!? – xander Mar 15 '17 at 13:59
  • 2
    How can a variable be incremented if it "doesn't have" an initial value? – chris Mar 15 '17 at 13:59
  • 2
    Any table of C++ operator precedence, for example [here](http://web.ics.purdue.edu/~cs240/misc/operators.html), will show you how to parse this expression. – davidbak Mar 15 '17 at 14:01
  • 1
    You might try `a = 0, 42;` to test comma. – Jarod42 Mar 15 '17 at 14:01
  • @user3415509 neither, `a` nor `b`, nor `c` have a value, from that perspective your assertion really makes no sense, i'd suggest reading [this](http://en.cppreference.com/w/cpp/language/ub) – George Mar 15 '17 at 14:03
  • The clue is in the error message. You need to give `b` and `c` an initial value. If you don't they could contain any random value. – Galik Mar 15 '17 at 14:03
  • http://www.studytonight.com/cpp/operators-and-their-types.php here's the source, last page. – user3415509 Mar 15 '17 at 14:04
  • As it happens, cppquiz has a question about this: http://cppquiz.org/quiz/question/120 – chris Mar 15 '17 at 14:04
  • 2
    @user3415509, That explanation is wrong in multiple ways. First, no comma operator is used in `int a,b,c;` Second, it's flat out incorrect regarding the behaviour of the second line. And right above that it says C++ has a `>>>` operator? It's super easy to verify that it doesn't. The unsigned right shift operator is simply `>>` applied to unsigned operands. – chris Mar 15 '17 at 14:06
  • "here's the source" - sadly, that example is incomplete. as you discovered, since your compiler is issuing error messages. Fix the error messages and try again. – davidbak Mar 15 '17 at 14:06
  • @chris - good catch on the comments at that tutorial "explaining" the behavior of both those lines - I didn't even read that far. Hopefully those errors aren't generally indicative of the quality of that tutorial, or the OP is going to have a hard time learning C++ there ... (Just read your revised comment pointing out the tutorial also claims there's an operator `>>>`. Yeah, I suggest the OP find a different - better! - tutorial! Or proceed to learn C++ by finding all the ways in which this one is wrong! – davidbak Mar 15 '17 at 14:07
  • Checking a few other pages, it looks like an extremely bad tutorial for C++. Not only does it contain errors, like in this example, but also non-standard terms like "equivalence operator" instead of "equal to" (lost in translation?). Please try to read [a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead! – Bo Persson Mar 15 '17 at 14:19
  • You know what else? It's also a bad example because of the use of the post-increment operators in the two expressions separated by the comment. The reason why that makes it a bad example is left as an exercise for you ... – davidbak Mar 15 '17 at 14:24
  • @davidbak it is funny, but link you gave for operator precedence is wrong – Slava Mar 15 '17 at 14:27
  • @Slava - I did that on purpose (\*) to prove you can't rely on random stuff you read on the internet. Even if it is the first result returned by google. (*) That's my story and I'm sticking to it ... – davidbak Mar 15 '17 at 14:36
  • @Slava - huh, I looked into it based on your remark and learned something new: that the precedence of the ternary conditional operator relative to assignment operators is different in C and C++. Thanks! (Making that an interview question though would be just nasty.) – davidbak Mar 15 '17 at 14:45
  • If you're looking for a better tutorial, I would suggest checking [C programming.com](http://www.cprogramming.com/) or [TutorialsPoint](https://www.tutorialspoint.com/cplusplus/index.htm); they don't get everything right, but I don't remember seeing any glaring errors in either of them. – Justin Time - Reinstate Monica Mar 15 '17 at 17:07

1 Answers1

0

Source you using is wrong multiple times.

int a,b,c; 
a=b++, c++;

first of all reading from uninitialized variables leads to UB, so you cannot predict what would be in a. Second, even if you would initialize b and c that expression is equal to:

(a=b++), c++;

to see behaviour predicted on that site you have to write:

a=( b++, c++ );

due to higher precedence of operator= over comma. Details can be found here

Slava
  • 43,454
  • 1
  • 47
  • 90