views:

206

answers:

5

Hi,

When you see code like this in C, what's the order of assignment?

int i = 0, var1, var2;

I don't understand the syntax...

+11  A: 

Only i is assigned the value zero.

var1 and var2 are uninitialized.

Mitch Wheat
Uninitialized might be better. We know what you mean, but "uninitialized" is not only far more common (a buzz word), but carries the weight it should have: reading them leads to undefined behavior. Technically, `i` is unassigned too, because it's never been assigned (with strict adherence to the language definition of "assignment") a value.
GMan
Good point updated. Thx.
Mitch Wheat
var1 and var2 are uninitialized may not be correct unless we know the overall context of this declaration. These could be initialized to 0 if the declaration is in namespace scope.
Chubsdad
@Chubsdad: I'm referring to that single declaration line.
Mitch Wheat
+4  A: 

There is only one assignment (i=0), the rest are definitions.

srean
That's initialization, not assignment.
GMan
@GMan you are correct. I am leaving it as it is though, for the record.
srean
+5  A: 

i is initialized to 0 whereas variables var1 and var2 are uninitialized and thus have unspecified values(if they are defined in a local scope).

Prasoon Saurav
'i' is assigned the value 0 is also not correct. 'i' is initialized with 0
Chubsdad
@Chubsdad: Yes I should have corrected that after reading AndreyT's answer.
Prasoon Saurav
+7  A: 

There's no "assignment" in your code whatsoever. It is a declaration of three variables of type int, of which one is initialized with zero. The = symbol is an integral part of initialization syntax, it has nothing to do with any "assignment". And since there's only one initialization there, there's really no question about any "order".

If that doesn't answer your question, clarify it.

AndreyT
Nice answer: + 1
Chubsdad