2

I simply have the line as follows:

   int player = 1, i, choice;

My question is, what is assigned to i and what is assigned to choice? I was always taught that a variable could not be assigned to multiple values.

I Hate School
  • 53
  • 1
  • 4
  • 1
    This is not comma operator at work here, people. And this is not a dupe of that question. &%$#!"$#"!! – jrok Aug 23 '13 at 20:03

6 Answers6

4

That code is equivalent to saying

int player = 1;
int i;
int choice;

You are not assigning 1, i, and choice into the integer variable player.

Chris Stauffer
  • 352
  • 1
  • 8
3

Nothing is assigned anywhere in this -- it's doing initialization not assignment.

player is obviously initialized to 1.

If this definition is at namespace scope so i and choice have static storage duration, then they will be zero-initialized.

If this definition is local to a function, i and choice will be default-initialized, which (in the case of int) means they aren't given a predictable value.

If this definition is inside a class, then i and choice can be initialized by a constructor. A constructor generated by the compiler will default-initialize them.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

They (i and choice) have unspecified values. They can be pretty much any value, really. Unless you explicitly set them to a value, they are given some "junk" value (typically whatever is already in that memory location that they've been assigned).

You're not assigning to player multiple times. You're creating 3 variables, and assigned 1 to player. It's the same as if you had written:

int player = 1;
int i;
int choice;
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
1

Think of this as:

int player = 1;
int i;
int choice;

In c++ you are allowed to declare multiple of the same data type on the same line, like

char c1, c2, c3;

and it is also accepted to give one of these a value in their declaration, like what your line is doing. So as simonc said, there will be no value in i or choice.

taronish4
  • 504
  • 3
  • 5
  • 13
  • In c++ you are allowed to declare multiple of the same data type on the same line, like. You are also in c – dhein Aug 23 '13 at 14:13
  • @Zaibis: Yes, you are also in C, but according to the tags, the question is only about C++. – Jerry Coffin Aug 23 '13 at 14:16
  • but as you specially mention "c++" it sounds as it would be else where not allowed, if you are refering to "its tagged as c++" just dont mention it explicitly ;P – dhein Aug 23 '13 at 14:20
  • I'm mentioning C++ so that people don't assume that this is common in every programming language. – taronish4 Aug 23 '13 at 15:28
1

What you have there is definition & initialization, definition, definition;

int player = 1;
int i;// = most probably 'random' trash from heap/stack memory
int choice;// = most probably 'random' trash from heap/stack memory
Sam
  • 7,778
  • 1
  • 23
  • 49
0

i and choice aren't defined. What happens is, you are saying: I want to declare player as int and asign 1 to it then a sequens interupt sign(,), says the type specifier has reference to the next sequenz. Means also to declare i and choice as int (but without asigning anythign)

dhein
  • 6,431
  • 4
  • 42
  • 74
  • 1
    I've never been so confused! –  Aug 23 '13 at 18:05
  • I don't think `,` means "sequence interrupt" in this context. – Etienne de Martel Aug 23 '13 at 18:06
  • @ Etienne de Martel exactly thats what a ',' does mean in the standard. And a sequence is finished, at the end of an full expression, so a single declaration is a sequence, so exactly thats what the ',' does, it interupts the sequence! – dhein Aug 23 '13 at 19:00