Increment

P

per9000

Hi, today I experimented with the increment/++ operator. It seems the
behavior depends on the compiler (I used cl/gcc + cs/mcs) and the
language (I tested C and C#).

Is the ++-operator well defined in all cases? I tested on this for-
loop and the results differ in c/cl, c/gcc and C#/(cs or mcs).

/*
** c1-c8 are all 1 before this loop
** b1-b8 contain only zeros
** a = 1,2,3...16
*/

for (i = 2; i < 5; i++, c3++, ++c7)
{
b1[c1] = a[c1++];
b2[c2++] = a[c2];
b3[c3] = a[c3];
b4[c4++] = a[c4++];

b5[c5] = a[++c5];
b6[++c6] = a[c6];
b7[c7] = a[c7];
b8[++c8] = a[++c8];
}

I understand that this coding-standard (f.x: "b2[c2++] = a[c2];") is
horrible and I will never again use increment on something that is
used more than once in a row. Are there any guides to "good coding
practice" when it comes to increment?

See details here: http://www.pererikstrandberg.se/blog/index.cgi?page=PlusPlusOperator

Thanks,
Per
 
G

Guest

Hi, today I experimented with the increment/++ operator. It seems the
behavior depends on the compiler (I used cl/gcc + cs/mcs) and the
language (I tested C and C#).

Is the ++-operator well defined in all cases?

No. For C, please see the FAQ at <http://c-faq.com/expr/
evalorder1.html> and the directly linked pages. For C#, that is OT in
this newsgroup, and while some people may know the answer, I expect
most won't. You'll get better results in a newsgroup dedicated to that
language.
 
P

per9000

No. For C, please see the FAQ at <http://c-faq.com/expr/
evalorder1.html> and the directly linked pages. For C#, that is OT in
this newsgroup, and while some people may know the answer, I expect
most won't. You'll get better results in a newsgroup dedicated to that
language.

Ok, thanks.

Quote of the day "if you don't care which cell of a[] gets written to,
why write code which seems to write to a[] at all?"

Pretty much gives a summary of what I was doing :)

/P
 
C

CBFalconer

per9000 said:
.... snip ...

b1[c1] = a[c1++];
b2[c2++] = a[c2];
b3[c3] = a[c3];
b4[c4++] = a[c4++];

b5[c5] = a[++c5];
b6[++c6] = a[c6];
b7[c7] = a[c7];
b8[++c8] = a[++c8];

Except for the 3 and 7 examples, all of these exhibit undefined
behaviour. DDT (don't do that).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top