473,320 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C FAQ 3.1

Hi all,
In CFAQ 3.1, it says:

A: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?

Q: ...the language in K&R suggests that the behavior of this expression
is unspecified, the C Standard makes the stronger statement that it is
undefined...

I think this is interesting. What metrics can we use to determine it is
undefined or unspecified? A similar case is:

f(a++, a);

People says the order of evaluation for function arguments is
undefined. Why can't it be unspecified?

Nov 14 '05 #1
15 1109
ccwork wrote:
Hi all,
In CFAQ 3.1, it says:

A: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?

Q: ...the language in K&R suggests that the behavior of this expression is unspecified, the C Standard makes the stronger statement that it is undefined...

I think this is interesting. What metrics can we use to determine it
is undefined or unspecified?
Any metric consistent with the Standard(s).
A similar case is:

f(a++, a);

People says the order of evaluation for function arguments is
undefined.
Forget what 'people' say, read what the Standard(s) say.

"The order of evaluation of the function designator, the actual
arguments, and subexpressions within the actual arguments is
unspecified, but there is a sequence point before the actual
call."
Why can't it be unspecified?


The real question is: Why _should_ it be specified?

--
Peter

Nov 14 '05 #2
"ccwork" <cc****@hotmail.com> wrote:
In CFAQ 3.1, it says:

A: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?
That's funny, in the web version that's question 3.2.
Q: ...the language in K&R suggests that the behavior of this expression
is unspecified, the C Standard makes the stronger statement that it is
undefined...

I think this is interesting. What metrics can we use to determine it is
undefined or unspecified? A similar case is:
If the Standard says the behaviour of some code unspecified, it's
unspecified. If the Standard doesn't define any behaviour, or explicitly
says the behaviour is undefined, it is undefined.
f(a++, a);

People says the order of evaluation for function arguments is
undefined. Why can't it be unspecified?


People say wrong; the _order_ of evaluation _is_ unspecified. That call
does invoke undefined behaviour, but not because of the order of
evaluation. Rather, it modifies a in the first argument, and also reads
a, not for the purpose of determining its new value, in the second
argument. IOW, it modifies a in one expression, and reads it in another
expression which isn't part of the first one. And this without an
intervening sequence point. _That_ is undefined. The order of evaluation
is indeed unspecified.

The difference comes into play when you call code such as

newvalue=compute(pop(&stack), pop(&stack));

where the value of compute depends on the order of the pop()s. This code
does not invoke undefined behaviour, because there's a sequence point
just before each call of pop() - but the order in which the pop()s are
called; and therefore, the order in which the arguments are popped off
the stack; and therefore, the values that get passes to computer; and
therefore, the new value of newvalue; is unspecified.

Richard
Nov 14 '05 #3
Peter Nilsson wrote:
ccwork wrote:
Hi all,

Why can't it be unspecified?

The real question is: Why _should_ it be specified?


Because it would clear up a lot of confusion, specify exactly what the
behavior should be. I am convinced we have undefined behavior because
K&R were too lazy or in a hurry to specify exactly what should happen in
these now undefined cases.
Nov 14 '05 #4
On Tue, 26 Apr 2005 12:04:15 +0000, Stan Milam wrote:
Peter Nilsson wrote:
ccwork wrote:
Hi all,

Why can't it be unspecified?

The real question is: Why _should_ it be specified?


Because it would clear up a lot of confusion, specify exactly what the
behavior should be. I am convinced we have undefined behavior because
K&R were too lazy or in a hurry to specify exactly what should happen in
these now undefined cases.


It is more likely because this freedom gives compilers extra scope for
optimisation. On some architectured the natural calling sequence might
favour left to right evaluation of arguments, on another ot might be right
to left. Some optimisers might find advantages in other arrangements. It
isn't that common for the order of evaluation to cause a problem so making
it unspecified is a reasonable choice.

Lawrence

Nov 14 '05 #5
>the C Standard makes the stronger statement that it is undefined...
I just want to known why? This makes the stronger??

Nov 14 '05 #6
In my VC 6.0
int i = 7;
mov dword ptr [ebp-4],7
printf("%d\n",i++*i++);
mov eax,dword ptr [ebp-4]
imul eax,dword ptr [ebp-4]
contrast another code
<int i =7;mov dword ptr [ebp-4],7 <printf("%d\n",++i*++i);mov eax,dword ptr [ebp-4]
add eax,1
mov dword ptr [ebp-4],eax
mov ecx,dword ptr [ebp-4]
add ecx,1
mov dword ptr [ebp-4],ecx
mov edx,dword ptr [ebp-4]
imul edx,dword ptr [ebp-4] From that ,i summarize :

We get the result regardless of the order of evaluation, no matter
what from right to left or reverse. Do like this need less mem, less
confussion. Most complier operate like this just i known.

Nov 14 '05 #7
Lawrence Kirby <lk****@netactive.co.uk> wrote:
On Tue, 26 Apr 2005 12:04:15 +0000, Stan Milam wrote:
Because it would clear up a lot of confusion, specify exactly what the
behavior should be. I am convinced we have undefined behavior because
K&R were too lazy or in a hurry to specify exactly what should happen in
these now undefined cases.


It is more likely because this freedom gives compilers extra scope for
optimisation. On some architectured the natural calling sequence might
favour left to right evaluation of arguments, on another ot might be right
to left. Some optimisers might find advantages in other arrangements.


In fact, near the end of the web page posted just this morning (in
<11**********************@f14g2000cwb.googlegroups .com>), a compiler
writer describes why his compiler uses neither left-to-right nor right-
to-left, but what he calls hardest-first ordering.

Richard
Nov 14 '05 #8

In article <pa****************************@netactive.co.uk> , Lawrence Kirby <lk****@netactive.co.uk> writes:
On Tue, 26 Apr 2005 12:04:15 +0000, Stan Milam wrote:

Because it would clear up a lot of confusion, specify exactly what the
behavior should be. I am convinced we have undefined behavior because
K&R were too lazy or in a hurry to specify exactly what should happen in
these now undefined cases.


It is more likely because this freedom gives compilers extra scope for
optimisation. On some architectured the natural calling sequence might
favour left to right evaluation of arguments, on another ot might be right
to left. Some optimisers might find advantages in other arrangements.


In fact, with modern processors, it's quite likely that the optimal
order of evaluation for some function calls will depend on what else
is happening in the surrounding code, so the order might be different
for different calls in a given TU.

With OOE processors and write-back memory caching, it's possible that
even the compiler doesn't know what order evaluation will actually
effectively take place. Should C compilers be forced to insert memory
barriers in code that evaluates function parameters, just so the
order of evaluation can be specified?

I suspect Stan's conviction is way off base, and both K&R and the
committee had excellent reasons for not defining what happens in
every situation.

--
Michael Wojcik mi************@microfocus.com

Company Secretary Finance Manager
1) Half-grey haired executives.
2) Must be waist-deep in their field of activities.
3) Must be having the know-how and the do-how of the latest
developments in their respective fields.
-- from "Appointments Vacant" section of Business India
Nov 14 '05 #9

ccwork wrote:
Hi all,
In CFAQ 3.1, it says:

A: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?


It is my understanding that the ++ operator, when placed after the
variable will increment the value of the variable *after* the entire
expression has been evaluated. As such, 49 is the exact value that I
would expect if I were to write this code. However, having said that, I
would hope that I would never write this code, since it is rather
awkward.

-Jason

Nov 14 '05 #10
Michael Wojcik wrote:
In article <pa****************************@netactive.co.uk> , Lawrence Kirby <lk****@netactive.co.uk> writes:

I suspect Stan's conviction is way off base, and both K&R and the
committee had excellent reasons for not defining what happens in
every situation.


Well, I hope I am not off base. I *love* the C language, but after
years of using the langauge, reading this newsgroup, and reading a lot
of books I have come to the conclusion that these unspecified areas are
the tell-tale remains of a hurriedly written compiler back in the
1970's. You can read for yourself in K&R 2 where the authors admit that
some of the precedence of operators are wrong. However, by the time
anyone really noticed too much code depended on existing precedence
rules. How many times have we heard, said, or written that if something
was "fixed" it would break a lot of existing code? It is sort of like
depenancy lines in a make file must start with a tab. I wish K&R had
taken the time to clarify some of the penumbras that exist in the
language. Of course, they most likely never dreamed the language would
be as popular as it is and on so many platforms.

One good thing can be said of this: you can claim some level of
expertise not only knowing what to do with the language, but knowing
what to avoid too.

Regards,
Stan Milam.
Nov 14 '05 #11
Jason wrote:
ccwork wrote:
Hi all,
In CFAQ 3.1, it says:

A: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?


It is my understanding that the ++ operator, when placed after the
variable will increment the value of the variable *after* the entire
expression has been evaluated. As such, 49 is the exact value that I
would expect if I were to write this code. However, having said that, I
would hope that I would never write this code, since it is rather
awkward.


Your understanding is wrong. The variable could be incremented at any
time before the next sequence point, which in this case is when printf
is called. Therefore the compiler could do something really simple like
this...

t = i
inc i
t = t * i
inc i
printf("%d\n", t)

I suspect it could do something even stranger if it was more efficient.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #12

On Tue, 26 Apr 2005, huanghuijin\@tom.com wrote:

In my VC 6.0
int i = 7;
mov dword ptr [ebp-4],7

This is not C.
contrast another code
<int i =7;
mov dword ptr [ebp-4],7

This is also not C.
From that ,i summarize : We get the result regardless of the order of evaluation,


Incorrect. In the line 'printf("%d", ++i*++i);', we're not even
guaranteed to get /any/ result, much less the same one always, much
less the /right/ one always. The line exhibits undefined behavior,
and in real life, it exhibits different behaviors on different
implementations. (A decent compiler will produce a warning message
for this code, but no compiler is required to.)
no matter
what from right to left or reverse. Do like this need less mem, less
confussion.
I don't quite know what you're trying to say there, but I bet you're
wrong. (I expect you are confused. Which just goes to show that you're
also wrong about the "less confusion" part.)
Most complier operate like this just i known.


You don't know much about "most compilers," then.

-Arthur
Nov 14 '05 #13
"Jason" <ma******@hotmail.com> wrote:
ccwork wrote:
Hi all,
In CFAQ 3.1, it says:

A: Under my compiler, the code

int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?


It is my understanding that the ++ operator, when placed after the
variable will increment the value of the variable *after* the entire
expression has been evaluated.


Your understanding is wrong. The code i++ does two things:
- it increases the value of i;
- it evaluates to the old value of i.
There are no requirements at all about the order in which the program
does these. If the computer can manage it, it's allowed to do them in
parallel.

As for the code i++ * i++, since it invokes undefined behaviour, the
computer is allowed to do both increases in parallel, and crash as a
result. This is actually a good thing: it allows the compiler to
optimise, for example, i++ * j++ as well, without worrying about
intricate expressions that just happen to modify the same object twice.

Richard
Nov 14 '05 #14

"Stan Milam" <st*****@swbell.net> wrote in message
news:37**************@newssvr30.news.prodigy.com.. .
Peter Nilsson wrote:
ccwork wrote:
Hi all,
Why can't it be unspecified?

<snip>
discusssion is about
int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?

I think this is interesting. What metrics can we use to determine it is
undefined or unspecified? A similar case is:

f(a++, a);
</snip>

The real question is: Why _should_ it be specified?


Because it would clear up a lot of confusion, specify exactly what the
behavior should be. I am convinced we have undefined behavior because
K&R were too lazy or in a hurry to specify exactly what should happen in
these now undefined cases.

Have you ever thought of writing _your_ own lanugage that *specifies* the
order.

Answer for
i = 7;
printf("%d\n", i++ * i++);

i++ is post increment,
after every thing is done, do increment.
so
step 1)
i * i
( two ++ operatore are pending,
step 2)
%d <== i * i = 49
now everything for the calculation is over,
do the two ++

Print 49,

Moral of the story,
i = 7;
printf("II = %d\n", i++ * i++);

k = 7;
printf("KK = %d\n", (++k) * (++k));

m = 7;
printf("MM = %d\n", (++m) * (m));

II = 49
KK = 64
MM = 56
Question 2)
Order of f(a++, a);
Have you ever thought of writing _your_ own lanugage that *specifies* the
order.
You are calling a function, f(), with two parameters,
where are these parameters going, STACK ? / Registers ?
(for VXworks first few parameters are in Registers )
The function f() may be external to your module.
And function f() may also be defined in some other lanuguage.
__stdcall is used to write dlls in M$ windows and define function types.
(i do not know complete theories for the same )
If the parameters are passed in stack, how are they passed? first paramter
at TOS, last parameter at TOS?,
after the function returns, who will wind up the stack? the one who called
the function ( main() ), or the called function ( f() ) ?

K&R were so goofed up when they did this and hence used a short term to
finish it off.

behaviour of f(a++,a) is undefined.........

--
Purnank
(I know what i am writing, but i do not know if you are thinking what i am
thinking )
Nov 14 '05 #15
Purnank wrote:

"Stan Milam" <st*****@swbell.net> wrote in message
news:37**************@newssvr30.news.prodigy.com.. .
Peter Nilsson wrote:
ccwork wrote:

>Hi all,

>Why can't it be unspecified?
<snip>
discusssion is about
int i = 7;
printf("%d\n", i++ * i++);
prints 49. Regardless of the order of evaluation, shouldn't it print
56?

I think this is interesting.
What metrics can we use to determine it is
undefined or unspecified?


It violates a "shall constraint" and is undefined.

N869
6.5 Expressions
[#2] Between the previous and next sequence point an object
shall have its stored value modified at most once by the
evaluation of an expression.
The real question is: Why _should_ it be specified?
Because it would clear up a lot of confusion,
specify exactly what the behavior should be.
I am convinced we have undefined behavior because
K&R were too lazy or in a hurry to specify exactly
what should happen in these now undefined cases.


It would only add to your confusion.
Code which exhibits undefined behavior like that,
is so substandard, that it is actually not C code.
It's just gibberish that looks like C code.

--
pete
Nov 14 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.