Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sequence points and printf() (http://www.velocityreviews.com/forums/t806486-sequence-points-and-printf.html)

arnuld 12-01-2011 06:06 AM

sequence points and printf()
 
OBJECTIVE: To know why its UB


#include <stdio.h>

int main(void)
{
char c='8';
int d=8;
printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

return 0;
}
================ OUTPUT ===================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘d’ may be undefined
test.c:7: warning: operation on ‘c’ may be undefined
[arnuld@dune C]$ ./a.out
9 9 56
[arnuld@dune C]$


gcc says its UB. The way I understand it is:

(1) d is incremented in 2nd argument without defining any sequence
points, hence you can not guarantee what printf() will print as value of
d.

(2) same for c, as its being incremented without defining any sequence
point.

But as per C-FAQs here: http://c-faq.com/expr/seqpoints.html

comma operator defined a sequence point and I see comma is present
between printf() arguments but I am not sure if this "comma" is called as
"comma operator" ?


--
arnuld
http://LispMachine.Wordpress.com

Alan Curry 12-01-2011 06:13 AM

Re: sequence points and printf()
 
In article <4ed7195d$0$294$14726298@news.sunsite.dk>,
arnuld <sunrise@invalid.address> wrote:
> printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

[...]
>
>comma operator defined a sequence point and I see comma is present
>between printf() arguments but I am not sure if this "comma" is called as
>"comma operator" ?


You hit on the problem right there. The function-argument-separating comma is
not an operator.

--
Alan Curry

Martin Ambuhl 12-01-2011 07:19 AM

Re: sequence points and printf()
 
On 12/1/2011 1:06 AM, arnuld wrote:

>
> But as per C-FAQs here: http://c-faq.com/expr/seqpoints.html
>
> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?


Commas between arguments are punctuation and not comma operators.


BartC 12-01-2011 11:18 AM

Re: sequence points and printf()
 


"arnuld" <sunrise@invalid.address> wrote in message
news:4ed7195d$0$294$14726298@news.sunsite.dk...

> printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);


> gcc says its UB. The way I understand it is:


> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?


The problem here is that the order the printf arguments are evaluated, is
not specified. The statement will give different results depending on
whether they're evaluated left-to-right or right-to-left (or starting in the
middle).

Saying it's 'UB' is a bit of an overreaction though, as the worst that can
happen *ought* to be that wrong results are printed, and that d ends up
being 1 more or less than you might expect.

--
Bartc


Willem 12-01-2011 11:59 AM

Re: sequence points and printf()
 
arnuld wrote:
) OBJECTIVE: To know why its UB
....
) printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);
....
) comma operator defined a sequence point and I see comma is present
) between printf() arguments but I am not sure if this "comma" is called as
) "comma operator" ?

This "comma" is definitely not called as "comma operator".

HTH, HAND.


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

Keith Thompson 12-01-2011 02:55 PM

Re: sequence points and printf()
 
Martin Ambuhl <mambuhl@earthlink.net> writes:
> On 12/1/2011 1:06 AM, arnuld wrote:
>> But as per C-FAQs here: http://c-faq.com/expr/seqpoints.html
>>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?

>
> Commas between arguments are punctuation and not comma operators.


Right.

A terminology quibble: comma *operators* are also punctuation.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

lawrence.jones@siemens.com 12-01-2011 02:59 PM

Re: sequence points and printf()
 
BartC <bc@freeuk.com> wrote:
>
> "arnuld" <sunrise@invalid.address> wrote in message
> news:4ed7195d$0$294$14726298@news.sunsite.dk...
>
> > printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

>
> The problem here is that the order the printf arguments are evaluated, is
> not specified. The statement will give different results depending on


Not just that, but there are no sequence points between the argument
evaluations. Both c and d are modified as well as being accessed
outside the modification expression, which makes the behavior completely
undefined.
--
Larry Jones

It's clear I'll never have a career in sports until I learn
to suppress my survival instinct. -- Calvin

Keith Thompson 12-01-2011 03:03 PM

Re: sequence points and printf()
 
"BartC" <bc@freeuk.com> writes:
> "arnuld" <sunrise@invalid.address> wrote in message
> news:4ed7195d$0$294$14726298@news.sunsite.dk...
>> printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

>
>> gcc says its UB. The way I understand it is:

>
>> comma operator defined a sequence point and I see comma is present
>> between printf() arguments but I am not sure if this "comma" is called as
>> "comma operator" ?

>
> The problem here is that the order the printf arguments are evaluated, is
> not specified. The statement will give different results depending on
> whether they're evaluated left-to-right or right-to-left (or starting in the
> middle).
>
> Saying it's 'UB' is a bit of an overreaction though, as the worst that can
> happen *ought* to be that wrong results are printed, and that d ends up
> being 1 more or less than you might expect.


No, the behavior is undefined.

C99 6.5p2:

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.

The above printf call doesn't violate the first sentence, but it
does violate the second. There is no sequence point between reading
d and modifying d *and* the value read is not used to compute the
value to be stored.

Something like

x = x + 1;

reads and writes x with no intervening sequence points, but its
behavior is well defined because the value that's read is used to
compute the value to be stored. Because of this, the read *must*
occur before the write, so no conflict can occur. (C201X expresses
this more clearly; it says that the read is "sequenced before"
the write, rather than leaving it implicit.)

In the printf call, the read and write of d are unsequenced
(can occur in either order), and the standard says this produces
undefined behavior.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Phil Carmody 12-01-2011 03:04 PM

Re: sequence points and printf()
 
"BartC" <bc@freeuk.com> writes:
> "arnuld" <sunrise@invalid.address> wrote in message
> news:4ed7195d$0$294$14726298@news.sunsite.dk...
>
> > printf("%d %d %d\n",d, d+=c>='0'&& c<='9', c++);

>
> > gcc says its UB. The way I understand it is:

>
> > comma operator defined a sequence point and I see comma is present
> > between printf() arguments but I am not sure if this "comma" is called as
> > "comma operator" ?

>
> The problem here is that the order the printf arguments are evaluated,
> is not specified. The statement will give different results depending
> on whether they're evaluated left-to-right or right-to-left (or
> starting in the middle).
>
> Saying it's 'UB' is a bit of an overreaction though, as the worst that
> can happen *ought* to be that wrong results are printed, and that d
> ends up being 1 more or less than you might expect.


This is a dangerous and sloppy way of thinking.

Arnuld is trying to learn C, we should be helping him to avoid
sloppy ways of thinking.

Anyway, I'd not say it's an over-reaction. An overreaction would be
someone going to their manager and saying something like "X is not
competent in the field for which he was hired. That's the second one
from the same consultancy in 6 months, can you tell resourcing there's
a problem?". Not that that's ever happened, nor did the one from 6
months earlier magically re-appear in a different team a year later.

Phil
--
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator

Seebs 12-01-2011 08:08 PM

Re: sequence points and printf()
 
On 2011-12-01, arnuld <sunrise@invalid.address> wrote:
> comma operator defined a sequence point and I see comma is present
> between printf() arguments but I am not sure if this "comma" is called as
> "comma operator" ?


It's not. The commas separating function call arguments are not
comma operators.

-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.


All times are GMT. The time now is 09:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.