Increment

2 views
Skip to first unread message

per9000

unread,
Apr 2, 2007, 4:48:04 PM4/2/07
to
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

--

Per Erik Strandberg
blog: http://www.pererikstrandberg.se/blog/
work: http://tomopt.com/tomnet/

Harald van Dijk

unread,
Apr 2, 2007, 4:55:29 PM4/2/07
to
On Apr 2, 8:48 pm, "per9000" <per9...@gmail.com> wrote:
> 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.

Ben Pfaff

unread,
Apr 2, 2007, 4:50:04 PM4/2/07
to
"per9000" <per...@gmail.com> writes:

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

No.

This is in the FAQ.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008

per9000

unread,
Apr 2, 2007, 5:07:17 PM4/2/07
to

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

CBFalconer

unread,
Apr 2, 2007, 9:09:59 PM4/2/07
to
per9000 wrote:
>
... 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).

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com

per9000

unread,
Apr 3, 2007, 2:38:48 AM4/3/07
to
> ... 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. ...

For the record: I posted a similar post in
microsoft.public.dotnet.languages.csharp here
http://groups.google.se/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/2c1535e97a28a279/3fa0b340ceac1c60

thanks again,
Per

Reply all
Reply to author
Forward
0 new messages