# Comma in CPP

This topic is 4355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What is the difference between:
int i = 0;

i = 1, i++;

And:
int i = 0;

i = 1;
i++;


##### Share on other sites
The former is less readable that the later. Other than that, there is no difference :)

Regards,

##### Share on other sites
I haven't used it much in this way (readability concerns in 90% of cases), but the comma operator basically allows multiple statements to be executed in order where one statement is expected.

so i = 1, i++; is literally executed as:
1: i = 1;
2: i++;

watch your brackets as i = (1, i++); is different from i= 1, i++;

int i = 0;

i = (1, i++);

returns the result 1... But that's because it executes like this:

1;
i++;
i = i;

So in this case if you hadn't initialized i first it would actually be incrementing i without the assignment as in the first example which could lead to very costly errors depending on implementation. You can see how this makes the entire situation more complicated than it needs to be and why it's needlessly obscure (not a great word for it, but meh) in this case.

Basically that's it.

Just be very cautious and only use it where necessary, these are contrived examples. Know that.

##### Share on other sites
int i=0;(1, i) = 1;

Is this valid?

##### Share on other sites
It is valid. But not good, don't do it.

things such as arguments don't work.

cout << 1,2 << endl;

is invalid syntactically because there are two values where one is expected, however (1,2) is fine because it returns a single value after being evaluated... In fact, the last value mentioned (2 in this case).

(1, i) = 1;

the last value, i is chosen for the assignment, if you swap the values you'll have problems assigning a value to a symbol or a const or whatever you want to call it.

##### Share on other sites
Quote:
 Original post by M2tM[about i = (1, i++);]1;i++;i = i;

Note that it may also execute as:

1;
int temp = i;
i++;
i = temp;

The order of execution between the = and the ++ operators is unspecified, therefore the code here is non-deterministic.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by M2tM[about i = (1, i++);]1;i++;i = i;

Note that it may also execute as:

1;
int temp = i;
i++;
i = temp;

The order of execution between the = and the ++ operators is unspecified, therefore the code here is non-deterministic.

Indeed, it may work on some configurations, it may not. Don't do it.

Thanks for the extra info, and yes, it would be the same problem as i = i++;

Now I have a question though, and it's a simple one but one I just haven't really looked into. Do brackets enable this to be safe?

i = (i++);

My gut reaction is no, especially if a temporary storage space is used exactly as you have there.

The other thing playing in my mind is that maybe the brackets ensure that the operation completes and somehow returns the complete value. I don't know if this is defined in the standards, I'd check it in google but I don't know what I'd search for.

*edit: It seems it goes based on something called "sequence points" I'll look that up, but feel free to supply a description or links.

##### Share on other sites
By the way, I am not going to "do it". I am just writting a small CPP like compiler.

##### Share on other sites
I find using the comma operator to be very useful in for loops:
for(int y=0, index=0 ; y<height ; ++y)    for(int x=0 ; x<width ; ++y, ++index)        buffer = calcSomething(x,y);

##### Share on other sites
Yeah, for loops are pretty much the best place for them.

sequence points

There we go... So no, i = (i++); is still undefined as I thought...

The () don't trigger a sequence point.

1. 1
2. 2
3. 3
Rutin
19
4. 4
5. 5

• 9
• 19
• 9
• 31
• 16
• ### Forum Statistics

• Total Topics
632617
• Total Posts
3007459

×