COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### operator precedence in c - clarifications needed

• Email
• Follow

```Hello friends,

I am quite familiar with C and have coded a lot. But I get stumped on
precedence rules, and I use paranthesis always. Can one of you out there
explain how the value of s is evaluated in the following code snippet?

int a=5,s=0;
s=(++a) + (a++) + (--a) + (a--);

```
 0
Reply pkp_0000 (2) 8/10/2008 5:51:10 PM

See related articles to this posting

```Prasad said:

> Hello friends,
>
> I am quite familiar with C and have coded a lot. But I get stumped on
> precedence rules, and I use paranthesis always. Can one of you out there
> explain how the value of s is evaluated in the following code snippet?
>
> int a=5,s=0;
> s=(++a) + (a++) + (--a) + (a--);

Precedence is not the issue here. The issue is order of evaluation, which
is unspecified in C. Because of that unspecified behaviour, the code falls
foul of the following dictum from 3.3 (Expressions) of C89:

"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the value
to be stored."

The wording is almost identical in C99:

"Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be read only to determine the value to
be stored."

What this means is that your code, which modifies a value more than once
between consecutive sequence points, exhibits undefined behaviour.
Therefore, any result - or none at all - is permissible.

Solution: break the statement up into multiple statements that express your
intent precisely. For example:

s = ++a;
s += a++;
s += --a;
s += a--;

(if that's what you meant).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
```
 0
Reply rjh (10790) 8/10/2008 6:50:08 PM

```Prasad wrote:
>
> I am quite familiar with C and have coded a lot. But I get stumped
> on precedence rules, and I use paranthesis always. Can one of you
> out there explain how the value of s is evaluated in the following
> code snippet?
>
> int a=5,s=0;
> s=(++a) + (a++) + (--a) + (a--);

That's an illegal statement.  It has no discernable value.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

```
 0
Reply cbfalconer (19194) 8/11/2008 1:30:33 AM

```CBFalconer said:

>>
>> I am quite familiar with C and have coded a lot. But I get stumped
>> on precedence rules, and I use paranthesis always. Can one of you
>> out there explain how the value of s is evaluated in the following
>> code snippet?
>>
>> int a=5,s=0;
>> s=(++a) + (a++) + (--a) + (a--);
>
> That's an illegal statement.

No, it isn't. Its behaviour is undefined, but it's not against the law.
Compilers aren't even obliged to diagnose it.

> It has no discernable value.

On the contrary, the problem with it is that it has at least two
discernable values.

For example, if we evaluate left-to-right, it is easy to see how we can get
a = 5, and s = 24.
If we evaluate right-to-left, again it is easy to see how we can get a = 5,
s = 16.

It is less easy to see how we might get other results - but if we bear in
mind that the compiler has licence to behave as if all the sub-expressions
are independent of each other, it becomes a little easier to imagine. It
is not difficult to find worked examples in mocked-up assembly language in
comp.lang.c archives, that demonstrate how a variety of results are
possible.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
```
 0
Reply rjh (10790) 8/11/2008 5:34:39 AM

```Richard Heathfield wrote:
>
>> Hello friends,
>>
>> I am quite familiar with C and have coded a lot. But I get stumped on
>> precedence rules, and I use paranthesis always. Can one of you out there
>> explain how the value of s is evaluated in the following code snippet?
>>
>> int a=5,s=0;
>> s=(++a) + (a++) + (--a) + (a--);
>
> Precedence is not the issue here. The issue is order of evaluation, which
> is unspecified in C. Because of that unspecified behaviour, the code falls
> foul of the following dictum from 3.3 (Expressions) of C89:
>
>    "Between the previous and next sequence point an object shall have its
> stored value modified at most once by the evaluation of an expression.
> Furthermore, the prior value shall be accessed only to determine the value
> to be stored."
>
> The wording is almost identical in C99:
>
> "Between the previous and next sequence point an object shall have its
> stored value modified at most once by the evaluation of an expression.
> Furthermore, the prior value shall be read only to determine the value to
> be stored."
>
> What this means is that your code, which modifies a value more than once
> between consecutive sequence points, exhibits undefined behaviour.
> Therefore, any result - or none at all - is permissible.
>
> Solution: break the statement up into multiple statements that express your
> intent precisely. For example:
>
> s = ++a;
> s += a++;
> s += --a;
> s += a--;

....or simply

s = 4 * (a + 1);

August
```
 0
Reply fusionfile1 (23) 8/11/2008 10:32:54 AM

```Richard Heathfield wrote:
> CBFalconer said:
>
>>> I am quite familiar with C and have coded a lot. But I get stumped
>>> on precedence rules, and I use paranthesis always. Can one of you
>>> out there explain how the value of s is evaluated in the following
>>> code snippet?
>>>
>>> int a=5,s=0;
>>> s=(++a) + (a++) + (--a) + (a--);
>> That's an illegal statement.
>
> No, it isn't. Its behaviour is undefined, but it's not against the law.
> Compilers aren't even obliged to diagnose it.
>
>> It has no discernable value.
>
> On the contrary, the problem with it is that it has at least two
> discernable values.
>
> For example, if we evaluate left-to-right, it is easy to see how we can get
> a = 5, and s = 24.
> If we evaluate right-to-left, again it is easy to see how we can get a = 5,
> s = 16.
>
> It is less easy to see how we might get other results - but if we bear in
> mind that the compiler has licence to behave as if all the sub-expressions
> are independent of each other, it becomes a little easier to imagine. It
> is not difficult to find worked examples in mocked-up assembly language in
> comp.lang.c archives, that demonstrate how a variety of results are
> possible.

Here's one:
You can also get ((6 + 5 + 4 + 5) = 20),
regardless of direction of evaluation,
and then (a) is incremented twice and decremented twice.

--
pete
```
 0
Reply pfiland (6614) 8/11/2008 11:39:42 AM

```August Karlstrom said:

> Richard Heathfield wrote:

<snip>

>> Solution: break the statement up into multiple statements that express
>> your intent precisely. For example:
>>
>> s = ++a;
>> s += a++;
>> s += --a;
>> s += a--;
>
> ...or simply
>
> s = 4 * (a + 1);

If that were his intent, yes. (It may not have been.)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
```
 0
Reply rjh (10790) 8/11/2008 12:07:43 PM

```Hello,

Thanks for all the clarifications. As should be obvious, I defenitely don't
want to use this code in any application. I posted this just out of
curiosity as I could not find an explanation for the value arrived at by the
compiler ( which is 20 ).

In another part of this thread, Pete has mentioned that 20 could be a
possible answer, though I couldn't make out how. Hope he will tell me. (or
am i being dumb? )

"Richard Heathfield" <rjh@see.sig.invalid> wrote in message
news:AaydndgM9pkctz3VnZ2dnUVZ8u2dnZ2d@bt.com...
> August Karlstrom said:
>
>> Richard Heathfield wrote:
>
> <snip>
>
>>> Solution: break the statement up into multiple statements that express
>>> your intent precisely. For example:
>>>
>>> s = ++a;
>>> s += a++;
>>> s += --a;
>>> s += a--;
>>
>> ...or simply
>>
>> s = 4 * (a + 1);
>
> If that were his intent, yes. (It may not have been.)
>
> --
> Richard Heathfield <http://www.cpax.org.uk>
> Email: -http://www. +rjh@
> "Usenet is a strange place" - dmr 29 July 1999

```
 0
Reply pkp_0000 (2) 8/11/2008 4:27:19 PM

```"Prasad" <pkp_0000@yahoo.com> wrote in message news:g7pp9i\$mcd\$1@aioe.org...
> Hello,
>
> Thanks for all the clarifications. As should be obvious, I defenitely
> don't want to use this code in any application. I posted this just out of
> curiosity as I could not find an explanation for the value arrived at by
> the compiler ( which is 20 ).
>
> In another part of this thread, Pete has mentioned that 20 could be a
> possible answer, though I couldn't make out how. Hope he will tell me. (or
> am i being dumb? )
>

You've received several good replies.  Now you might make
a little effort and figure out the rest yourself.

-- Bob Day

```
 0
Reply xxxxxx7 (40) 8/11/2008 5:11:15 PM

```Prasad wrote:
> Hello,
>
> Thanks for all the clarifications. As should be obvious, I defenitely don't
> want to use this code in any application. I posted this just out of
> curiosity as I could not find an explanation for the value arrived at by the
> compiler ( which is 20 ).
>
> In another part of this thread, Pete has mentioned that 20 could be a
> possible answer, though I couldn't make out how. Hope he will tell me. (or
> am i being dumb? )

int a=5,s=0;
s=(++a) + (a++) + (--a) + (a--);

You can also get ((6 + 5 + 4 + 5) = 20),
regardless of direction of evaluation,
and then (a) is incremented twice and decremented twice.

Followup-To:comp.lang.c

You have to understand the concept of sequence points
and values and side effects.

N869
Evaluation of an expression may produce side effects.
At  certain  specified
points in the execution sequence called sequence points, all
side effects of previous evaluations shall be  complete  and
no  side  effects of subsequent evaluations shall have taken
place.

A statement terminator semicolon is an example of sequence point.
Assignment is not a sequence point.
Assignment is an example of side effect.

Expression of object type,
can be completely described in terms of value and side effects.
Between two sequence points,
evaluations and side effects can occur in any order.

The only sequence point here:
s=(++a) + (a++) + (--a) + (a--);
is the semicolon.

And so, (a) starts with an initial value of 5,
it is evaluated 4 times, incremented twice and decremented twice.

Here's the meaning of the code:

(++a) means to increment (a).
The value of the expression is the value after the increment,
but the increment can happen any time
after the previous sequence point
and before the next sequence point.
The initial value of (a) is 5.
The value after the increment is 6.
The value of (++a) is 6.

(a++) means to increment (a).
The value of the expression is the value before the increment,
but the increment can happen any time
after the previous sequence point
and before the next sequence point.
The initial value of (a) is 5.
The value before the increment is 5.
The value of (a++) is 5.

(--a) means to decrement (a).
The value of the expression is the value after the decrement,
but the decrement can happen any time
after the previous sequence point
and before the next sequence point.
The initial value of (a) is 5.
The value after the decrement is 4.
The value of (--a) is 4.

(a--) means to decrement (a).
The value of the expression is the value before the decrement ,
but the decrement can happen any time
after the previous sequence point
and before the next sequence point.
The initial value of (a) is 5.
The value before the decrement is 5.
The value of (++a) is 5.

6 + 5 + 4 + 5 = 20

However, it is not the case that the result is limited
to what you can construe out of that code.
The way it is, is that code like that,
is considered by the C standard committee
to be so needlessly complicated in meaning,
that it is classified as causing undefined behavior,
which means that no matter what the program does for that code,
it's not a compiler bug, it's your bug.

N869
Between the previous and next sequence point an  object
shall  have  its  stored  value modified at most once by the
evaluation of an expression.

--
pete
```
 0
Reply pfiland (6614) 8/11/2008 9:29:17 PM

```Prasad wrote:
>
> Thanks for all the clarifications. As should be obvious, I
> defenitely don't want to use this code in any application. I
> posted this just out of curiosity as I could not find an
> explanation for the value arrived at by the compiler ( which is
> 20 ).
>
> In another part of this thread, Pete has mentioned that 20 could
> be a possible answer, though I couldn't make out how. Hope he
> will tell me. (or am i being dumb? )

As I recall you posted some code that had undefined behavior.
Anything can happen, so suggesting mechanisms is pointless.
Running it could also erase all files on your hard disk.

with) the quoted material to which you reply, after snipping all
irrelevant material.  See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://members.fortunecity.com/nnqweb/>  (newusers)

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

```
 0
Reply cbfalconer (19194) 8/11/2008 11:10:52 PM

```Richard Heathfield wrote:
> CBFalconer said:
>>>
>>> I am quite familiar with C and have coded a lot. But I get stumped
>>> on precedence rules, and I use paranthesis always. Can one of you
>>> out there explain how the value of s is evaluated in the following
>>> code snippet?
>>>
>>> int a=5,s=0;
>>> s=(++a) + (a++) + (--a) + (a--);
>>
>> That's an illegal statement.
>
> No, it isn't. Its behaviour is undefined, but it's not against the
> law. Compilers aren't even obliged to diagnose it.
>
>> It has no discernable value.
>
> On the contrary, the problem with it is that it has at least two
> discernable values.

No, since the behaviour is undefined, it can also erase all files
on all disks.  I consider this an undiscernable value.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

```
 0
Reply cbfalconer (19194) 8/11/2008 11:13:46 PM

```In article <48A0C7AA.A0CB3231@yahoo.com>, cbfalconer@yahoo.com says...
> Richard Heathfield wrote:
> > CBFalconer said:
> >>>
> >>> I am quite familiar with C and have coded a lot. But I get stumped
> >>> on precedence rules, and I use paranthesis always. Can one of you
> >>> out there explain how the value of s is evaluated in the following
> >>> code snippet?
> >>>
> >>> int a=5,s=0;
> >>> s=(++a) + (a++) + (--a) + (a--);
> >>
> >> That's an illegal statement.
> >
> > No, it isn't. Its behaviour is undefined, but it's not against the
> > law. Compilers aren't even obliged to diagnose it.
> >
> >> It has no discernable value.
> >
> > On the contrary, the problem with it is that it has at least two
> > discernable values.
>
> No, since the behaviour is undefined, it can also erase all files
> on all disks.  I consider this an undiscernable value.
>
I can't help but feel that this argument is unproductive. Yes - it is
understood that according to the "standard" it is undefined. But in the
real world "undefined" is not the equivalent of "anything can happen",
but rather that those who write the compilers do not have to take any
particular care to ensure that the line will compile sensibly.

Similarly, accessing an out of bounds array element is an example of
undefined behaviour> But I think that we can all guarantee that there is
no compiler designed to delete all your files and send a threatening e-
mail to your boss every time you make a minor typo in a program.

I feel Richard's explanation for why it is undefined is far more useful
that the often parroted "anything can happen" comment.

Mike
```
 0
Reply m.fee (53) 8/11/2008 11:50:57 PM

```mike <m.fee@irl.cri.replacethiswithnz> writes:

> In article <48A0C7AA.A0CB3231@yahoo.com>, cbfalconer@yahoo.com says...
>> Richard Heathfield wrote:
>> > CBFalconer said:
>> >>>
>> >>> I am quite familiar with C and have coded a lot. But I get stumped
>> >>> on precedence rules, and I use paranthesis always. Can one of you
>> >>> out there explain how the value of s is evaluated in the following
>> >>> code snippet?
>> >>>
>> >>> int a=5,s=0;
>> >>> s=(++a) + (a++) + (--a) + (a--);
>> >>
>> >> That's an illegal statement.
>> >
>> > No, it isn't. Its behaviour is undefined, but it's not against the
>> > law. Compilers aren't even obliged to diagnose it.
>> >
>> >> It has no discernable value.
>> >
>> > On the contrary, the problem with it is that it has at least two
>> > discernable values.
>>
>> No, since the behaviour is undefined, it can also erase all files
>> on all disks.  I consider this an undiscernable value.
>>
> I can't help but feel that this argument is unproductive. Yes - it is
> understood that according to the "standard" it is undefined. But in the
> real world "undefined" is not the equivalent of "anything can happen",
> but rather that those who write the compilers do not have to take any
> particular care to ensure that the line will compile sensibly.

Hence the anything can happen.  And anything do happen, in particular
when the OS doesn't provide any protection.  If the line doesn't
compile sensibly, you can get random bytes in the execution path, and
therefore really random behavior, including disk erasing.  On a unix
system you could hope for a segfault, but while unicies are more or
less resistant to random code, some holding as little as less than one
second, and some up to a few minutes, before the kernel is crashed.

> Similarly, accessing an out of bounds array element is an example of
> undefined behaviour> But I think that we can all guarantee that there is
> no compiler designed to delete all your files and send a threatening e-
> mail to your boss every time you make a minor typo in a program.

If the out of bound access is writting the instructions to that effect
in the code after the PC, of course it will do that, as any virus
knows it very well.

> I feel Richard's explanation for why it is undefined is far more useful
> that the often parroted "anything can happen" comment.

--
__Pascal Bourguignon__                     http://www.informatimago.com/

READ THIS BEFORE OPENING PACKAGE: According to certain suggested
versions of the Grand Unified Theory, the primary particles
constituting this product may decay to nothingness within the next
four hundred million years.
```
 0
Reply pjb (7870) 8/12/2008 5:46:21 AM

```CBFalconer said:

> Richard Heathfield wrote:
>> CBFalconer said:
>>>>
<snip>
>>>>
>>>> int a=5,s=0;
>>>> s=(++a) + (a++) + (--a) + (a--);
>>>
>>> That's an illegal statement.
>>
>> No, it isn't. Its behaviour is undefined, but it's not against the
>> law. Compilers aren't even obliged to diagnose it.
>>
>>> It has no discernable value.
>>
>> On the contrary, the problem with it is that it has at least two
>> discernable values.
>
> No, since the behaviour is undefined, it can also erase all files
> on all disks.  I consider this an undiscernable value.

Yes, I am happy to agree that it has an undiscernable value *as well*. But
that's not what you first said. You said it had "no discernable value". I
can discern (at least) two values, and others here have demonstrated that
they, too, can discern (at least) two values. Therefore, the claim that it
has "no discernable value" is (at least) doubly wrong. Whilst you
correctly point out that it also has "an undiscernable value", this is
irrelevant to my argument.

Whilst the Standard doesn't eliminate by fiat the risk of the compiler
erasing all files on all disks as a result of this particular line of
code, that risk is nevertheless so laughably small as to be not worth
considering. If you choose to guard against it, to be logically consistent
you should also guard against much more likely risks such as that of your
key employees winning the national lottery and quitting, or an alien
spaceship crashing into your office building.

The risk of getting the wrong value (that is, a value you weren't
expecting) is considerably higher, and worth guarding against by
specifying the calculation more carefully.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
```
 0
Reply rjh (10790) 8/12/2008 6:17:50 AM

```Richard Heathfield wrote:
> CBFalconer said:
>
>>> I am quite familiar with C and have coded a lot. But I get stumped
>>> on precedence rules, and I use paranthesis always. Can one of you
>>> out there explain how the value of s is evaluated in the following
>>> code snippet?
>>>
>>> int a=5,s=0;
>>> s=(++a) + (a++) + (--a) + (a--);
>> That's an illegal statement.
>
> No, it isn't. Its behaviour is undefined, but it's not against the law.
> Compilers aren't even obliged to diagnose it.

Neither are they obliged to compile it.

Running software that can translate and execute a C program,
is what an implementation of C is.

Whether or not running software
can translate and execute a text file
containing the line identified by CBFalconer as "illegal",
has no bearing on whether or not
that running software is a C implementation.

Code that contains that line, isn't a C program,
it's just gibberish that looks like a C program;
and if your compiler does compile it,
then you have fooled your compiler.

Followup-To:comp.lang.c

--
pete
```
 0
Reply pfiland (6614) 8/12/2008 6:43:57 AM

```In article <MPG.230b9782617dd26b98968c@news.comnet.net.nz>, mike
<m.fee@irl.cri.replacethiswithnz> writes
>I can't help but feel that this argument is unproductive. Yes - it is
>understood that according to the "standard" it is undefined. But in the
>real world "undefined" is not the equivalent of "anything can happen",
>but rather that those who write the compilers do not have to take any
>particular care to ensure that the line will compile sensibly.

True. But, for example, the resulting code might corrupt variables
outside those appearing in the expression. Or stack pointers. Which in
turn could result in one system call being transmuted into another; say
"open file" into "delete file".

>Similarly, accessing an out of bounds array element is an example of
>undefined behaviour> But I think that we can all guarantee that there is
>no compiler designed to delete all your files and send a threatening e-
>mail to your boss every time you make a minor typo in a program.

No, but look at how many viruses and other threads rely on buffer
overflow.

--
Clive D.W. Feather                       | Home: <clive@davros.org>
Tel: +44 20 8495 6138 (work)             | Web:  <http://www.davros.org>
Fax: +44 870 051 9937                    | Work: <clive@demon.net>
```
 0
Reply clive4154 (11) 8/13/2008 4:57:56 PM

```In article <obZ2elMUKxoIFwOk@romana.davros.org>, clive@on-the-
train.demon.co.uk says...
> In article <MPG.230b9782617dd26b98968c@news.comnet.net.nz>, mike
> <m.fee@irl.cri.replacethiswithnz> writes
> >I can't help but feel that this argument is unproductive. Yes - it is
> >understood that according to the "standard" it is undefined. But in the
> >real world "undefined" is not the equivalent of "anything can happen",
> >but rather that those who write the compilers do not have to take any
> >particular care to ensure that the line will compile sensibly.
>
> True. But, for example, the resulting code might corrupt variables
> outside those appearing in the expression. Or stack pointers. Which in
> turn could result in one system call being transmuted into another; say
> "open file" into "delete file".
>
> >Similarly, accessing an out of bounds array element is an example of
> >undefined behaviour> But I think that we can all guarantee that there is
> >no compiler designed to delete all your files and send a threatening e-
> >mail to your boss every time you make a minor typo in a program.
>
> No, but look at how many viruses and other threads rely on buffer
> overflow.
>
I'm not trying to be arguementative here, but just trying to point out
that any typo or logic error or uncorrected input error could do the
same thing, as could the cosmic-ray induced corruption of a single bit
anywhere in a program. But in practice, it is rather unlikely. I have
managed to destabilise my PC on numerous occasions due to a bug-induced
crash (maybe because I am stuck with Windows), but have never done any
significant damage. So to me a bug due to an 'undefined' statement is no
better or worse than any other programming bug.

But there seems to be a culture within the c programming community that
almost demonises the _evil_ of undefined statements. The other common
offhand remark that I have seen more than once is "It's undefined - it
could make demons fly out of your nose". Now I accept that standards-
based programming is probably a good thing: if you can write a
conforming program then you can (in theory at least) predict what it
will do. (Well actually you can't, as Turing demonstrated!) But in the
real world, with typos, programmer error, compiler bugs, operating
system bugs, hardware failure - an undefined statement is probably no
worse than any other bug.
Mike
```
 0
Reply m.fee (53) 8/13/2008 11:59:48 PM

```mike wrote:
) I'm not trying to be arguementative here, but just trying to point out
) that any typo or logic error or uncorrected input error could do the
) same thing,  ... <snip>
) <snip> ...   But in the
) real world, with typos, programmer error, compiler bugs, operating
) system bugs, hardware failure - an undefined statement is probably no
) worse than any other bug.

There is a significant issue you have overlooked that separates
undefined-behaviour-bugs.  Often, on one compiler, UB predictably does
the same thing.  A programmer could think that it's *supposed* to do that
and use it.  Testing cannot find the bug because it's _not there_ in the
compiled code, just in the source code.

The bug will only surface when the project switches to a new compiler.
(Or even a new version of the same compiler).

So, two significant issues, actually...
- Programmers may not know it's UB and come to rely on it.
- Bugs are only in source code and may not appear in compiled code.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
```
 0
Reply willem (1478) 8/14/2008 6:55:31 AM

```On Aug 11, 6:10=A0pm, CBFalconer <cbfalconer@yahoo.com> wrote:
>
> > Thanks for all the clarifications. As should be obvious, I
> > defenitely don't want to use this code in any application. I
> > posted this just out of curiosity as I could not find an
> > explanation for the value arrived at by the compiler ( which is
> > 20 ).
>
> > In another part of this thread, Pete has mentioned that 20 could
> > be a possible answer, though I couldn't make out how. Hope he
> > will tell me. (or am i being dumb? )
>
> As I recall you posted some code that had undefined behavior.
> Anything can happen, so suggesting mechanisms is pointless.
> Running it could also erase all files on your hard disk.

AFAICS, that could only happen under two circumstances:

(a) The compiler produces instructions to erase all files in the hard
disk. (The creator of the compiler is mentally insane.)
(b) The OS is insecure enough to let process misbehavior cause this
operation. (The OS is useless.)

Personally, I know of no such (useless) compiler, and of no such
(useless) OS.

In any other case, such as a side effect produced by the OS because of
an unrelated event, the "anything can happen" fact also applies if the
behavior *were* defined.

<snip>

Sebastian

```
 0
Reply s0suk3 (372) 8/15/2008 3:05:17 AM

19 Replies
40 Views

Similar Articles

12/12/2013 3:27:00 PM
page loaded in 5891 ms. (0)

Similar Artilces:

FAQ 7.7 Why do Perl operators have different precedence than C operators? #4 337078
This message is one of several periodic postings to comp.lang.perl.misc intended to make it easier for perl programmers to find answers to common questions. The core of this message represents an excerpt from the documentation provided with Perl. -------------------------------------------------------------------- 7.7: Why do Perl operators have different precedence than C operators? Actually, they don't. All C operators that Perl copies have the same precedence in Perl as they do in C. The problem is with operators that C doesn't have, especially functions that give a l...

FAQ 7.7 Why do Perl operators have different precedence than C operators? #6 355535
This is an excerpt from the latest version perlfaq7.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 7.7: Why do Perl operators have different precedence than C operators? Actually, they don't. All C operators that Perl copies have the same precedence in Perl as they do in C. The problem is with operators that ...

FAQ 7.7 Why do Perl operators have different precedence than C operators? #7 468417
This is an excerpt from the latest version perlfaq7.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 7.7: Why do Perl operators have different precedence than C operators? Actually, they don't. All C operators that Perl copies have the same precedence in Perl as they do in C. The problem is with oper...

operating on files in C++ need help
I am a c programmer and new to C++. In C, I am familiar with some operations on file. Now when shifting from c to c++, using iostream, I have no idea how to perform the following operations 1. check if a file or directory exists or not 2. delete all the files under a given directory 3. rename Rex_chaos wrote: > I am a c programmer and new to C++. In C, I am familiar with some > operations on file. Now when shifting from c to c++, using iostream, I > have no idea how to perform the following operations > > 1. check if a file or directory exists or not Directory: no standard wa...

Operator precedence table in C++ standard
Hello, I am trying to find a reference in the C++ standard that specifies the precedence of operators. I don't see it, yet most C++ texts will have such a chart so I am wondering from where they are getting their information. Can someone please point out where/how in the standard the precedence of operators is specified? Thank you, -------------------- Ivan http://www.0x4849.net -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] Ivan Novick wrote: > Hello, > > I am trying to find a refer...

Help needed with a C++ syntax (legal in C, but not C++)
Greetings, I'm trying to use a vender-supplied API in my C++ program. The vender's API is in C and it contains something similar to: type int INT; struct S { int i; double INT; /* OK in C, not C++ */ } According to a C/C++ book (C, A reference manual by S Harbison and G. Steele JR), the above struct is actually legal in C, as typedef names can be redeclared as objects in inner scopes. However, in C++ it is not permitted to do so within a structure or union -- which are scopes -- if the original typedef name has been used in the structure or union already. The problem I'm f...

C, C++, C#.NET Programmers need in Michigan
VisionIT is seeking up to 25 C Programmers (C, C++, C#.NET) for its client in Lansing and Troy, Michigan. Positions are on a 6 month contract to hire basis and are paying \$42.31 per hour. Our client has been awarded seven new Medicaid contracts in Kentucky, Oregon, Alabama, Florida, Wisconsin, Connecticut and Massachusetts and they require resources to help get things up and running. Required Skills: 3-5 years, C/C++/C# programming experience, plus basic UNIX Command knowledge, and familiarity with SQL VisionIT Wants You! VisionIT is all about connecting the right candidate with the right ...

c/c++ source code needed
I've been scowering the net (i.e., musicdsp.org, google, etc) for full source code implementations of these effects: reverb, echo/delay, distortion and chorus. Any implementations will do as long as they have at least 2 or 3 parameters that I can change. What I'm looking for basically is a function that takes an inbuffer and returns the processed outbuffer. What I've found mostly is a lot of pseudocode and stuff. I'm a novice to DSP and such and need this for a larger project without having to learn all this stuff to write the code myself. Any help would be appreciated. L...

Help is needed for C program compiled by C++
Hi, I need your help. I have a legend C program that works well after it was compiled under Visial Studio 6. Now a new version of Visial Studio 2005, V8 is installed on my machine. And I failed to generate its *.exe file. What I have done is: 1. Create a project under C++; 2. Select 32-bit window console project; 3. Add my *.c files into it. 4. Compile it. Error information: When I included statement: #include "stdafx.h" Error information: Error 1 fatal error C1853: 'Debug\Filter.pch' precompiled header file is from a previous version of the compiler, or the precompiled ...

.ini file C/C++ lib needed
Hi, I'd need a C, preferably C++ library which can be used to parse windows-style .ini files. Does someone know one? Google, apt-cache and yast aren't much of a help. Thanks Uwe -- I feel sorry for your brain... all alone in that great big head... Uwe Mayer <merkosh@hadiko.de> writes: > Hi, > > I'd need a C, preferably C++ library which can be used to parse > windows-style .ini files. Does someone know one? > Google, apt-cache and yast aren't much of a help. IIRC, there's something out there called libini. I haven't used it, but if memory se...

Automating C/C++ Coding Standards for any Operating System
Abraxas Software Understanding YOUR GOALS & Using CodeCheck Implementing Corporate Source Code Guidelines C/C++ Source Code GuideLine Automation The goals of CodeCheck are: 1 To create a standard which will enable you to provide the customer with a quality product in a timely manner. 2 To promote standardization of software development among programmers. 3 To act as a learning tool for new programmers. 4 To act as a reference tool for all levels of programmers. 5 To promote a team approach among programmers. 6 To help programmers create readable, re-usable and maintainable software. 7 T...

what does ## operator does in C ?
I just want to know the function of ## operator. On Mon, 6 Sep 2010 10:16:13 -0700 (PDT), knight <vimal3271@gmail.com> wrote: >I just want to know the function of ## operator. It's the token pasting operator. It allows concatenation of tokens in macros based on the invocation of the macro. Goggle for more complete explanation. ...

C / C++ extensions: need all the tips/tricks you can give me
Hi! I'm looking for all available sources of information on developing ruby extensions. I'm not a C language expert, but I've worked some years ago as a junior C/C++ developer. For that reason, I think I could get in business if I find the good resources inside the mess of google results for "writing ruby C extensions" query... I would like to hear your suggestions, experiences, gotchas, etc... on the subject as links to web pages or directly here for both: * Essentials C (and C++) language knowledge that I will need * Specific ruby extensions tips * I need mostly info...

Operator Precedence
Source : ''The C Programming Language" By Kernighan & Richie "Some of the operators have the wrong precedence" In which situations this happen? "Spidey" <amalhashim@gmail.com> wrote in message news:1133071422.109264.247670@g14g2000cwa.googlegroups.com... > Source : ''The C Programming Language" By Kernighan & Richie > > "Some of the operators have the wrong precedence" > > In which situations this happen? > > In which situations this happen? This sounds like you've interpreted this statement...

C/C++ speed optimization bible/resources/pointers needed!
C/C++ speed optimization bible/resources/pointers needed! Hi all, I am in the middle of programming to solve an engineering problem where the speed is huge concern. The project involving lots of numerical integration and then there are several loops/levels of optimization on top of the function evaluation engine. As you probably know, the key to a successful optimization is a fast underlying objective function evaluator. The faster it is, the more promising the optimization result(perhaps global optimal). However our project requires many numerical integrations which prohibits us from making...

I need help files for using KDE c, c++ development enviroment
I need help files for using KDE c, c++ development environment please recommend some good books on the subject john ...