问题
In one of my assignments I am seeing this line used:
int index = -1, k;
Im not sure what is happening when there are to entries for the one variable. What exactly is the variable "index" holding when it has two entries?
回答1:
In C, the comma operator , has lower precedence than the assignment operator =. As such, the expression
int index = -1, k;
is parsed as
//The parentheses are not legal in C, but it's what the parser does.
int ((index = -1), k);
You see, that the line declares variables of type int. The first one of which is called index and is initialized to -1, the second one is called k and is not initialized.
You can find a good overview of the operator precedences here:
https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence
Note that the comma operator is the very last one in that list!
Likewise, you may see C-code like this, or similar:
if(condition) foo += 7, doSomething();
while(i += 2, i < 42) ...;
This is equivalent to
if(condition) {
foo += 7;
doSomething();
}
i += 2;
while(i < 42) {
...
i += 2;
}
but much more terse (many C programmers like terseness!). Again, in both cases the comma operator serves to fuse two things into a single statement, which avoids writing a full block {} and prevents repetition of the increment i += 2.
Whether such uses of the comma operator are good or bad is a matter of taste and circumstance. But you can be certain to find all possible uses of it in the wild.
回答2:
This is the definition of 2 variables, both of the same type int: index and k. Only index is initialized with -1, and k is left un-initialized.
回答3:
It's just defining two variables of type int, one of them (index) is initialized to -1, the other (k) is left uninitialized.
This is btw bad style because it really looks confusing.
回答4:
As several other people have said, this is a declaration of two variables. It is 100% equivalent to
int index = -1;
int k;
and modern coding style would encourage you to write it that way. However - there is a lot of old C out there, and in the 1980s and 1990s, grouping variable declarations was the preferred style. Pick a random dusty deck and I guarantee you you'll see things like
register f, r, (*hstat)(), (*istat)(), (*qstat)();
int status;
or maybe
extern char level;
int newtry[31],newother[31],*r,*q,*p,n,sum,first;
int ii,lastwhite,lastred;
So you need to understand what it means. Sadly, they can get quite confusing, e.g.
int const* a, b;
which is equivalent to
const int *a;
int b;
and that sort of thing is why modern coding styles prefer one variable per declaration.
(Why did people prefer to group declarations back in the day? I don't know. Personally, I would guess that it helps you see more code at once on your 80x25 glass tty, but I've never actually had that experience, so.)
回答5:
In this code "index" and "k" both are integer type variable and the variable "index" is assigned by -1.(i.e. the value of index is -1). It's called variable initialization.
回答6:
Assuming this is code within the scope of a function:
int index = -1, k;
will do just the same as
int index = -1;
int k;
or the same as
int index, k;
index = -1;
even if the code is not equivalent by the C standard, no known C compiler in the world would treat these three code blocks any different .
In C you can declare multiple variables of the same type at once (int index, k;) or you define a variable (declare and initialize a variable at the same time, int index = -1;), or you can do both at once, declare multiple and initialize them or just one of them as in your case (int index = -1, k;).
来源:https://stackoverflow.com/questions/33159979/c-variable-confusion