Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #10100 > unrolled thread
| Started by | Mazen <neurohacks@gmail.com> |
|---|---|
| First post | 2011-09-10 12:36 -0700 |
| Last post | 2011-09-13 19:22 +0200 |
| Articles | 20 on this page of 35 — 18 participants |
Back to article view | Back to comp.lang.c
Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 12:36 -0700
Re: Unexpected output China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-10 12:59 -0700
Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-10 16:42 -0400
Re: Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 17:51 -0700
Re: Unexpected output Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-10 20:38 -0600
Re: Unexpected output pete <pfiland@mindspring.com> - 2011-09-10 22:41 -0400
Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-11 09:35 -0400
Re: Unexpected output gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-11 13:39 +0000
Re: Unexpected output "BartC" <bc@freeuk.com> - 2011-09-11 15:26 +0100
Re: Unexpected output Paul N <gw7rib@aol.com> - 2011-09-11 12:08 -0700
Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-11 17:43 -0400
Re: Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 13:24 -0700
Re: Unexpected output China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-10 15:02 -0700
Re: Unexpected output Kenneth Brody <kenbrody@spamcop.net> - 2011-09-12 10:01 -0400
Re: Unexpected output arni <arnmin@gmail.com> - 2011-09-10 13:31 -0700
Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-10 16:49 -0400
Re: Unexpected output arni <arnmin@gmail.com> - 2011-09-10 14:13 -0700
Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-10 17:26 -0400
Re: Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 17:59 -0700
Re: Unexpected output Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-10 20:52 -0600
Re: Unexpected output China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-10 19:55 -0700
Re: Unexpected output Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-11 06:03 -0700
Re: Unexpected output James Kuyper <jameskuyper@verizon.net> - 2011-09-11 14:04 -0400
Re: Unexpected output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-11 11:22 +0100
Re: Unexpected output Keith Thompson <kst-u@mib.org> - 2011-09-11 11:01 -0700
Re: Unexpected output Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-13 14:09 +0300
Re: Unexpected output Seebs <usenet-nospam@seebs.net> - 2011-09-13 06:44 +0000
Re: Unexpected output "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-09-10 17:16 -0400
Re: Unexpected output "BartC" <bc@freeuk.com> - 2011-09-10 23:20 +0100
Re: Unexpected output August Karlstrom <fusionfile@gmail.com> - 2011-09-11 22:55 +0200
Re: Unexpected output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-11 22:51 +0100
Re: Unexpected output pete <pfiland@mindspring.com> - 2011-09-11 22:25 -0400
Re: Unexpected output August Karlstrom <fusionfile@gmail.com> - 2011-09-12 15:34 +0200
Re: Unexpected output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-12 19:55 +0100
Re: Unexpected output August Karlstrom <fusionfile@gmail.com> - 2011-09-13 19:22 +0200
Page 1 of 2 [1] 2 Next page →
| From | Mazen <neurohacks@gmail.com> |
|---|---|
| Date | 2011-09-10 12:36 -0700 |
| Subject | Unexpected output |
| Message-ID | <8787e0b5-b4c3-463f-bd9a-6ffc30034950@x29g2000prh.googlegroups.com> |
Hello,
Following is a simple program:
#include <stdio.h>
int main(void) {
int i = 0, j, k;
j = k = 2;
printf("%d %d\n", i=+j, i=-j);
return 0;
}
The output is
2 2
Shouldn't the second expression be -2 rather than 2? I'm confused.
Your insights pls. Thanks!
[toc] | [next] | [standalone]
| From | China Blue Corn Chips <chine.bleu@yahoo.com> |
|---|---|
| Date | 2011-09-10 12:59 -0700 |
| Message-ID | <chine.bleu-E38952.12592910092011@news.eternal-september.org> |
| In reply to | #10100 |
In article <8787e0b5-b4c3-463f-bd9a-6ffc30034950@x29g2000prh.googlegroups.com>,
Mazen <neurohacks@gmail.com> wrote:
> Hello,
>
> Following is a simple program:
>
> #include <stdio.h>
>
> int main(void) {
> int i = 0, j, k;
> j = k = 2;
>
> printf("%d %d\n", i=+j, i=-j);
>
> return 0;
> }
>
> The output is
> 2 2
>
> Shouldn't the second expression be -2 rather than 2? I'm confused.
> Your insights pls. Thanks!
The order of evaluation of function arguments is left to the implementation.
--
I remember finding out about you, |With the nutty taste of wild hickory nuts.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room | Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.
Celle est une langue. C'est francais et tres, tres sexuel.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-10 16:42 -0400 |
| Message-ID | <j4gi57$taa$1@dont-email.me> |
| In reply to | #10101 |
On 9/10/2011 3:59 PM, China Blue Corn Chips wrote:
> In article<8787e0b5-b4c3-463f-bd9a-6ffc30034950@x29g2000prh.googlegroups.com>,
> Mazen<neurohacks@gmail.com> wrote:
>[...]
>> printf("%d %d\n", i=+j, i=-j);
>> [...]
>> The output is
>> 2 2
>>
>> Shouldn't the second expression be -2 rather than 2? I'm confused.
>> Your insights pls. Thanks!
>
> The order of evaluation of function arguments is left to the implementation.
It's more serious than that: The behavior is undefined. The
line in question tries to assign to `i' twice without an intervening
sequence point, and the C language makes no promises whatsoever about
what might happen in consequence.
C has two distinct uses for the comma: as an operator, and as
a separator. The comma operator involves a defined order and a
sequence point: `f = x,y' means "first evaluate x and ignore the
value, sequence point, then evaluate y and yield its value, that
value is assigned to f." The comma separator just separates the
elements of a list, without implying any kind of order and without
imposing sequence points. (Unfortunately, the two uses look
distressingly similar.)
In the offending line above, both commas are separators rather
than operators, so no sequence points divide the argument evaluations
from each other. In particular, the two assignments to `i' are not
so divided, hence the undefined behavior.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Mazen <neurohacks@gmail.com> |
|---|---|
| Date | 2011-09-10 17:51 -0700 |
| Message-ID | <d3f78e46-caac-4746-a28f-9c7b80a09858@glegroupsg2000goo.googlegroups.com> |
| In reply to | #10103 |
Thanks, Eric. So, how can one achieve the desired result and guarantee a defined behavior?
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2011-09-10 20:38 -0600 |
| Message-ID | <1bpqj76bwj.fsf@snowball.wb.pfeifferfamily.net> |
| In reply to | #10115 |
Mazen <neurohacks@gmail.com> writes:
> Thanks, Eric. So, how can one achieve the desired result and guarantee
> a defined behavior?
In short, "don't do that".
Getting your desired result depends (of course) on what result you
desired. Assuming your printf() was meant to be
printf("%d %d\n", i = +j, i = -j);
(so you were using an unnecessary '+', as opposed to using the old =+
syntax for += ), then even if it were defined it would be silly. Why
exactly did you assign a value to i, and then assign another value to
it?
If your intent was to print the value of j, print the value of -j, and
set i to -j, here's something that will do it:
printf("%d %d\n", j, i = -j);
If your intent was something else, well, you'd have to do something
else.
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2011-09-10 22:41 -0400 |
| Message-ID | <4E6C1FEA.232D@mindspring.com> |
| In reply to | #10115 |
Mazen wrote:
>
> Thanks, Eric. So, how can one achieve the desired result and guarantee a defined behavior?
#include <stdio.h>
int main(void) {
int i = 0, j, k;
j = k = 2;
i = -j;
printf("%d %d\n", j, i);
return 0;
}
--
pete
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-11 09:35 -0400 |
| Message-ID | <j4idfv$bi5$1@dont-email.me> |
| In reply to | #10115 |
On 9/10/2011 8:51 PM, Mazen wrote:
> Thanks, Eric. So, how can one achieve the desired result and guarantee a defined behavior?
What is the desired result?
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2011-09-11 13:39 +0000 |
| Message-ID | <j4idmq$k84$3@news.xmission.com> |
| In reply to | #10131 |
In article <j4idfv$bi5$1@dont-email.me>,
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
>On 9/10/2011 8:51 PM, Mazen wrote:
>> Thanks, Eric. So, how can one achieve the desired result and guarantee
>a defined behavior?
>
> What is the desired result?
That was clearly stated in the OP, but I forget it at the moment. I think
it was "2 2".
Anyway, the best way to fix the program is to replace the printf() statement
(and most of the declarations) with:
puts("2 2");
--
Some of the more common characteristics of Asperger syndrome include:
* Inability to think in abstract ways (eg: puns, jokes, sarcasm, etc)
* Difficulties in empathising with others
* Problems with understanding another person's point of view
* Hampered conversational ability
* Problems with controlling feelings such as anger, depression
and anxiety
* Adherence to routines and schedules, and stress if expected routine
is disrupted
* Inability to manage appropriate social conduct
* Delayed understanding of sexual codes of conduct
* A narrow field of interests. For example a person with Asperger
syndrome may focus on learning all there is to know about
baseball statistics, politics or television shows.
* Anger and aggression when things do not happen as they want
* Sensitivity to criticism
* Eccentricity
* Behaviour varies from mildly unusual to quite aggressive
and difficult
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2011-09-11 15:26 +0100 |
| Message-ID | <j4igg1$u4r$1@dont-email.me> |
| In reply to | #10115 |
"Mazen" <neurohacks@gmail.com> wrote in message
news:d3f78e46-caac-4746-a28f-9c7b80a09858@glegroupsg2000goo.googlegroups.com...
> Thanks, Eric. So, how can one achieve the desired result and guarantee a
> defined behavior?
Possibly something like:
printf("%d", i=+j);
printf(" %d\n", i=-j);
Or, if you don't care about the value of i at the end (and above, the first
assignment to i has no purpose):
printf("%d %d\n", +j, -j);
would work as well.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Paul N <gw7rib@aol.com> |
|---|---|
| Date | 2011-09-11 12:08 -0700 |
| Message-ID | <03877e30-335e-4348-9207-c9274d1f43e1@j19g2000yqc.googlegroups.com> |
| In reply to | #10103 |
On Sep 10, 9:42 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote: > C has two distinct uses for the comma: as an operator, and as > a separator. The comma operator involves a defined order and a > sequence point: `f = x,y' means "first evaluate x and ignore the > value, sequence point, then evaluate y and yield its value, that > value is assigned to f." Just to be picky, won't that expression actually assign x to f, as the = operator has a higher precedence than the , operator? (And yes, I know the standard doesn't actually call it precedence.)
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-11 17:43 -0400 |
| Message-ID | <j4ja33$9kk$1@dont-email.me> |
| In reply to | #10138 |
On 9/11/2011 3:08 PM, Paul N wrote:
> On Sep 10, 9:42 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
>> C has two distinct uses for the comma: as an operator, and as
>> a separator. The comma operator involves a defined order and a
>> sequence point: `f = x,y' means "first evaluate x and ignore the
>> value, sequence point, then evaluate y and yield its value, that
>> value is assigned to f."
>
> Just to be picky, won't that expression actually assign x to f, as the
> = operator has a higher precedence than the , operator? (And yes, I
> know the standard doesn't actually call it precedence.)
Oops! Serves me right for editing my example ("for clarity," of
course) after writing it -- and after ceasing to think ...
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Mazen <neurohacks@gmail.com> |
|---|---|
| Date | 2011-09-10 13:24 -0700 |
| Message-ID | <9f4702aa-9b1a-40e0-85eb-4bff12109f0a@a14g2000prl.googlegroups.com> |
| In reply to | #10101 |
On Sep 10, 12:59 pm, China Blue Corn Chips <chine.b...@yahoo.com>
wrote:
> In article <8787e0b5-b4c3-463f-bd9a-6ffc30034...@x29g2000prh.googlegroups.com>,
>
>
>
>
>
>
>
>
>
> Mazen <neuroha...@gmail.com> wrote:
> > Hello,
>
> > Following is a simple program:
>
> > #include <stdio.h>
>
> > int main(void) {
> > int i = 0, j, k;
> > j = k = 2;
>
> > printf("%d %d\n", i=+j, i=-j);
>
> > return 0;
> > }
>
> > The output is
> > 2 2
>
> > Shouldn't the second expression be -2 rather than 2? I'm confused.
> > Your insights pls. Thanks!
>
> The order of evaluation of function arguments is left to the implementation.
>
> --
> I remember finding out about you, |With the nutty taste of wild hickory nuts.
> Everyday my mind is all around you,| I'm whoever you want me to be.
> Looking out from my lonely room | Annoying Usenet one post at a time.
> Day after day. | At least I can stay in character.
> Celle est une langue. C'est francais et tres, tres sexuel.
Not sure I understood what you mean.
[toc] | [prev] | [next] | [standalone]
| From | China Blue Corn Chips <chine.bleu@yahoo.com> |
|---|---|
| Date | 2011-09-10 15:02 -0700 |
| Message-ID | <chine.bleu-C32D01.15021210092011@news.eternal-september.org> |
| In reply to | #10105 |
In article <9f4702aa-9b1a-40e0-85eb-4bff12109f0a@a14g2000prl.googlegroups.com>,
Mazen <neurohacks@gmail.com> wrote:
> > > printf("%d %d\n", i=+j, i=-j);
> >
> > > return 0;
> > > }
> >
> > > The output is
> > > 2 2
> >
> > > Shouldn't the second expression be -2 rather than 2? I'm confused.
> > > Your insights pls. Thanks!
> >
> > The order of evaluation of function arguments is left to the
> > implementation.
> >
> > --
> > I remember finding out about you, |With the nutty taste of wild hickory
> > nuts.
> > Everyday my mind is all around you,| I'm whoever you want me to
> > be.
> > Looking out from my lonely room | Annoying Usenet one post at a
> > time.
> > Day after day. | At least I can stay in
> > character.
> > Celle est une langue. C'est francais et tres, tres sexuel.
>
> Not sure I understood what you mean.
It might be implemented as
i = -2;
i = 2;
printf("...",i,i);
or
i = 2;
i = -2;
printf("...",i,i);
or
t1 = 2;
t2 = -2;
i = t1;
printf("...",t1,t2);
or a variety of other ways.
The comma as parameter separator is not the same comma as expression sequencer.
--
I remember finding out about you, |With the nutty taste of wild hickory nuts.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room | Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.
Celle est une langue. C'est francais et tres, tres sexuel.
[toc] | [prev] | [next] | [standalone]
| From | Kenneth Brody <kenbrody@spamcop.net> |
|---|---|
| Date | 2011-09-12 10:01 -0400 |
| Message-ID | <2didnYMqYbc4jfPTnZ2dnUVZ_tWdnZ2d@bestweb.net> |
| In reply to | #10101 |
On 9/10/2011 3:59 PM, China Blue Corn Chips wrote:
> In article<8787e0b5-b4c3-463f-bd9a-6ffc30034950@x29g2000prh.googlegroups.com>,
> Mazen<neurohacks@gmail.com> wrote:
>
>> Hello,
>>
>> Following is a simple program:
>>
>> #include<stdio.h>
>>
>> int main(void) {
>> int i = 0, j, k;
>> j = k = 2;
>>
>> printf("%d %d\n", i=+j, i=-j);
>>
>> return 0;
>> }
>>
>> The output is
>> 2 2
>>
>> Shouldn't the second expression be -2 rather than 2? I'm confused.
>> Your insights pls. Thanks!
>
> The order of evaluation of function arguments is left to the implementation.
And, more importantly, modifying "i" twice without a sequence point between
is UB.
--
Kenneth Brody
[toc] | [prev] | [next] | [standalone]
| From | arni <arnmin@gmail.com> |
|---|---|
| Date | 2011-09-10 13:31 -0700 |
| Message-ID | <743aa762-b7e7-4595-8c95-67ae93433e65@k15g2000yqd.googlegroups.com> |
| In reply to | #10100 |
On Sep 10, 10:36 pm, Mazen <neuroha...@gmail.com> wrote:
> Hello,
>
> Following is a simple program:
>
> #include <stdio.h>
>
> int main(void) {
> int i = 0, j, k;
> j = k = 2;
>
> printf("%d %d\n", i=+j, i=-j);
>
> return 0;
>
> }
>
> The output is
> 2 2
>
> Shouldn't the second expression be -2 rather than 2? I'm confused.
> Your insights pls. Thanks!
Shouldn't it be i+=j and i-=j?.. Then you'll get 2 -2.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-10 16:49 -0400 |
| Message-ID | <j4gig9$ufd$1@dont-email.me> |
| In reply to | #10102 |
On 9/10/2011 4:31 PM, arni wrote:
> On Sep 10, 10:36 pm, Mazen<neuroha...@gmail.com> wrote:
>> [...]
>> printf("%d %d\n", i=+j, i=-j);
>> [...]
>> return 0;
> [...]
> Shouldn't it be i+=j and i-=j?.. Then you'll get 2 -2.
Or not: The behavior would still be undefined, and for the
same reason as before.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | arni <arnmin@gmail.com> |
|---|---|
| Date | 2011-09-10 14:13 -0700 |
| Message-ID | <ddd0d158-7ebc-4216-9ba9-5409d84015b4@l2g2000vbn.googlegroups.com> |
| In reply to | #10104 |
On Sep 10, 11:49 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 9/10/2011 4:31 PM, arni wrote:
>
> > On Sep 10, 10:36 pm, Mazen<neuroha...@gmail.com> wrote:
> >> [...]
> >> printf("%d %d\n", i=+j, i=-j);
> >> [...]
> >> return 0;
> > [...]
> > Shouldn't it be i+=j and i-=j?.. Then you'll get 2 -2.
>
> Or not: The behavior would still be undefined, and for the
> same reason as before.
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid
Depends on compiler/optimizer implementation. Anyway, writing code
like that is a very bad practice, you can not be sure what's going to
happen in the end :)
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-10 17:26 -0400 |
| Message-ID | <j4gkn9$dd6$1@dont-email.me> |
| In reply to | #10106 |
On 9/10/2011 5:13 PM, arni wrote:
> On Sep 10, 11:49 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
>> On 9/10/2011 4:31 PM, arni wrote:
>>
>>> On Sep 10, 10:36 pm, Mazen<neuroha...@gmail.com> wrote:
>>>> [...]
>>>> printf("%d %d\n", i=+j, i=-j);
>>>> [...]
>>>> return 0;
>>> [...]
>>> Shouldn't it be i+=j and i-=j?.. Then you'll get 2 -2.
>>
>> Or not: The behavior would still be undefined, and for the
>> same reason as before.
>
> Depends on compiler/optimizer implementation.
The C language is defined by an International Standard, not by
the quirks of any individual compiler. The Standard does not define
any specific behavior for the offending line, nor even any limits on
the possible behaviors.
In particular, there's no guarantee that "you'll get 2 -2."
A particular implementation is free to add (or not) its own
definitions to cover cases the Standard leaves undefined -- but
then you're no longer talking about "C," but about "Frobozz Magic
C Version 4.2 optimization level 3 with #pragma DWIM in effect."
> Anyway, writing code
> like that is a very bad practice, you can not be sure what's going to
> happen in the end :)
Exactly: "What's going to happen" is not defined by the C language.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | Mazen <neurohacks@gmail.com> |
|---|---|
| Date | 2011-09-10 17:59 -0700 |
| Message-ID | <945abad2-7294-4e0a-8dff-76afcd6d27aa@y39g2000prd.googlegroups.com> |
| In reply to | #10109 |
On Sep 10, 2:26 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 9/10/2011 5:13 PM, arni wrote:
>
> > On Sep 10, 11:49 pm, Eric Sosman<esos...@ieee-dot-org.invalid> wrote:
> >> On 9/10/2011 4:31 PM, arni wrote:
>
> >>> On Sep 10, 10:36 pm, Mazen<neuroha...@gmail.com> wrote:
> >>>> [...]
> >>>> printf("%d %d\n", i=+j, i=-j);
> >>>> [...]
> >>>> return 0;
> >>> [...]
> >>> Shouldn't it be i+=j and i-=j?.. Then you'll get 2 -2.
>
> >> Or not: The behavior would still be undefined, and for the
> >> same reason as before.
>
> > Depends on compiler/optimizer implementation.
>
> The C language is defined by an International Standard, not by
> the quirks of any individual compiler. The Standard does not define
> any specific behavior for the offending line, nor even any limits on
> the possible behaviors.
>
> In particular, there's no guarantee that "you'll get 2 -2."
>
> A particular implementation is free to add (or not) its own
> definitions to cover cases the Standard leaves undefined -- but
> then you're no longer talking about "C," but about "Frobozz Magic
> C Version 4.2 optimization level 3 with #pragma DWIM in effect."
>
> > Anyway, writing code
> > like that is a very bad practice, you can not be sure what's going to
> > happen in the end :)
>
> Exactly: "What's going to happen" is not defined by the C language.
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid
I have learned the attempting to assign a value to a variable and
fetching the value of it in the same expression will produce an
undefined behaviour. What are some of the other cases that produce
undefined behaviour? This is interesting!
Thanks!
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2011-09-10 20:52 -0600 |
| Message-ID | <1blitv6bah.fsf@snowball.wb.pfeifferfamily.net> |
| In reply to | #10116 |
Mazen <neurohacks@gmail.com> writes:
>
> I have learned the attempting to assign a value to a variable and
> fetching the value of it in the same expression will produce an
> undefined behaviour. What are some of the other cases that produce
> undefined behaviour? This is interesting!
That isn't what you should have learned, because that's perfectly well
defined. If your printf() had been
printf("%d ", i = +j);
printf("%d\n", i = -j);
it would have printed the 2 -2 you expected -- though the first
assignment to i would still have been silly, and doing the second
assignment in the printf() would still have been useless obfuscation.
What was undefined was the order in which your three arguments
to printf() were evaluated, in a context where different orders of
evaluation would give different results.
I would be very surprised if anybody actually knows how many ways there
are to provoke undefined output. I supposed searching the standard for
the word "undefined" would help, but of course that would only tell you
all the ways the standard actually says something is undefined, and miss
all the possible things you could do that just don't have definitions.
But, frankly, the only reason to want to know what isn't defined is to
avoid doing it. And "don't write obviously silly code" (like your
original printf() ) is one of the most basic rules for doing that.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web