increment

P

per9000

Hi, yesterday I experimented with the increment/++ operator. It seems
some behavior is not defined in C (as I was informed when posting on
Comp.Lang.C) - does the same go for C#? Is it clear what f.x: "b2[c2+
+] = a[c2];" does in C#? Is it possible that a compiler, when going
from C# to CIL, could make "different" interpretations of my code.

I compiled with cs for .NET 1.x and 2.x and also mcs with identical
result. But gcc and cl produced different result from similar c-code
(the origin of my question).

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

My post in Comp.Lang.C:
http://groups.google.se/group/comp.lang.c/browse_thread/thread/716804e05dc001a9/5851243b04c893e5

Thanks,
Per
 
J

Jon Skeet [C# MVP]

Hi, yesterday I experimented with the increment/++ operator. It seems
some behavior is not defined in C (as I was informed when posting on
Comp.Lang.C) - does the same go for C#? Is it clear what f.x: "b2[c2+
+] = a[c2];" does in C#?
Absolutely.

Is it possible that a compiler, when going
from C# to CIL, could make "different" interpretations of my code.

Only if it's broken.

The C# 1 spec (simpler than 2 or 3!) is available in handy HTML form
here:
http://www.jaggersoft.com/csharp_standard/index.htm
The order of evaluation is specified. There are *some* things the C#
standard doesn't specify (where it makes sense not to) but things like
the above are well-specified.

However, I'd still argue that it's very, *very* rarely a good idea to
write code which requires you to be a language lawyer to read it. It's
nice to know that the C# spec guarantees the order of execution, but
readers are likely to find it easier if you just make it obvious what
will happen.

Jon
 

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

Top