2

I am new to C/C++ and I noticed my program calculating wrong values. I found the problem to be my understanding of how the declaration of variables works in C/C++. x,y,z = 1,2,3 works fine in Python where as x,y,z = 1 does not. int x,y,z = 1,2,3 doesnt not work in C/C++ but int x,y,z = 1 does, kind of, since cout << x; outputs 16 and I expected it to be 1.

#include <iostream>
using namespace std;

int main() {

    int x, y, z = 1;
    cout << x;
    
    return 0;

}

// The output is 16

What is the operation int x,y,z = 1 doing, if not assigning 1 to each?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
ExileCode
  • 33
  • 2
  • 4
    It assigns 1 to z and leaves x and y uninitialized. – drescherjm Aug 24 '21 at 00:20
  • If that's what it meant, how would you interpret `int x = 1, y, z;` and `int x = 1, y, z = 2`? – Brian61354270 Aug 24 '21 at 00:21
  • 1
    `int x = 1, y, z;` means init `x` to be 1 and others uninitilaised. `int x = 1, y, z = 2` means init `x` to 1, `z` to 2 and leave `y` unintialised. – kaylum Aug 24 '21 at 00:26
  • The [general behaviour](https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator) of a comma in C++ is evaluate left hand side and discard result then evaluate right hand side and keep result. – user4581301 Aug 24 '21 at 00:28
  • 1
    @user4581301 - The commas in variable declarations are not the comma operator. – Peter Aug 24 '21 at 00:36
  • 2
    This question is evidence supporting my view that Python fails to teach students the critical difference between initialization and assignment that exists in computer science. – Kaz Aug 24 '21 at 00:49

2 Answers2

7

It is shorthand for:

int x;
int y;
int z = 1;

x and y are left unassigned.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Ah, yeah that makes perfect sense now. But if x and y are unassigned, why do they both output 16? – ExileCode Aug 24 '21 at 00:39
  • 1
    See: [What happens in C when you print a declared, but unassigned variable?](https://stackoverflow.com/questions/32769922/what-happens-in-c-when-you-print-a-declared-but-unassigned-variable) and [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/questions/11962457/why-is-using-an-uninitialized-variable-undefined-behavior) and [Why does the C standard leave use of indeterminate variables undefined?](https://stackoverflow.com/questions/3248118/why-does-the-c-standard-leave-use-of-indeterminate-variables-undefined) – John Kugelman Aug 24 '21 at 00:44
  • @ExileCode It's a bug to read the value of a variable that you have not assigned a value to. Fix the bug and the mystery will go away. – David Schwartz Aug 24 '21 at 01:03
  • @JohnKugelman Hm, the documentaion confuses me. From what I read just now, it is to optimize the runtime. But surely deleting or disregarding the variable is faster than assigning a "garbage value" to it. And if there is no use case for this "bug" (the unassigned variables), why is this operation even allowed at all, instead of spitting out an error and this faulty variable doesn't get created in the first place, so it's not even possible for the user to overlook a coding error which might cause a performance decrease? – ExileCode Aug 24 '21 at 01:58
  • Consider this a glimpse into a lower level of programming. The garbage value hasn't been assigned to the variable; it's a *result* of nothing having been assigned. Why is it not an error? I agree, it should be. Many newer languages have learned to be stricter and will error out on this type of construct. C and C++ are older and more permissive. Lots of things will compile that shouldn't. Unfortunately, in C++ you cannot rely on the compiler catching every mistake. – John Kugelman Aug 24 '21 at 05:50
  • Oh, yeah now that makes sense, since C/C++ is always called robust language I should've expected this. But the concept of garbage values still seems a little arbitrary to me, because when I tried `int x; cout < – ExileCode Aug 24 '21 at 15:42
  • Uninitialized means the variable will get whatever value happened to be in that memory cell from whatever was there before. It could be anything. That could mean it's some random different value every time, but it could also be some consistent yet arbitrary value like 16 or 0. The bottom line is that it's undefined behavior to print an uninitialized variable, so don't read anything into its value when you do. – John Kugelman Aug 24 '21 at 20:25
  • Think of it RAM like an abandoned apartment full of furniture, clothes, and other assorted flotsam. The last tenant left without cleaning up and you, a new variable, move in. What do you find? Maybe baby toys. Maybe a stained rug. Maybe it's spotless because the last person was a neat freak. Who knows? Does it mean anything that there are baby toys strewn about? Nope, not a thing. Nobody cleaned up, that's all. The apartment is uninitialized. If you declare two variables instead of one, well maybe you get the stained rug apartment instead of the baby toy apartment. What's it mean? Nothing. – John Kugelman Aug 24 '21 at 20:34
2

int x,y,z = 1 initializes ONLY z to 1, x and y are left uninitialized.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770