2

I am a beginner in C. When I try to run the following code :

#include <stdio.h>

    int main(void) {

    int a = 3, b;

    b = printf("%d %d", a, a++);
    a = printf(" %d", b);
    printf(" %d", a);

    return 0;
}

It shows me :

error: operation on 'a' may be undefined [-Werror=sequence-point]
b = printf("%d %d", a, a++);
                        ^

But here I am changing the value of a only once. Why is there a sequence point error then ?

I am using -Wall -Werror flags.

chris
  • 60,560
  • 13
  • 143
  • 205
Crazy-Fool
  • 96
  • 1
  • 13
  • 1
    similar Question count === integer overflow! This question was asked already many many times! I think you would have been much faster doing a quick google search instead of the question – Rizier123 Apr 04 '15 at 18:29
  • @Rizier123 I did but I got only printf("%d %d %d", a++, ++a, a) type of questions :/ – Crazy-Fool Apr 04 '15 at 18:34
  • That's the same! And there are tones of those! – Rizier123 Apr 04 '15 at 18:40
  • @Rizier123 but here I am changing the variable only once. That's why I am unable to get it. – Crazy-Fool Apr 04 '15 at 18:51
  • BUT you use the variable twice and in one case you increment it! But you don't know which one gets evaluated first! – Rizier123 Apr 04 '15 at 18:52
  • A sequence point error occurs when I am trying to change the value of a variable more than once without a sequence point between the two operations ? Isn't it ? – Crazy-Fool Apr 04 '15 at 19:00
  • There certainly is a duplicate, but I can't find it currently. The point here isn't modifying several times, but modifying and reading (where the value isn't read only to determine the new value of the object) without an intervening sequence point. – mafso Apr 04 '15 at 21:41
  • @mafso I would have been very happy if I was able to get the answer from a duplicate. Anyhow, what I am unable to grasp is that, how does the ambiguity in the order of evaluation in printf causes the sequence point error. – Crazy-Fool Apr 05 '15 at 05:38
  • @Crazy-Fool: [This answer](http://stackoverflow.com/a/4176333/1741125) (C++, unfortunately, but C and C++ are basically the same here) under 2) in the last section addresses this. – mafso Apr 05 '15 at 10:53
  • [This one](http://stackoverflow.com/questions/19033354/i-can-not-understand-some-sentences-in-c99) (for C99)? – mafso Apr 05 '15 at 11:01
  • @mafso I actually read the fist one. But what currently bothers me is the fact that the sequence point error is due to the uncertainty in the order of evaluation of arguments of printf. – Crazy-Fool Apr 05 '15 at 13:09
  • @Crazy-Fool: What bothers you about that? If it were sequenced, everything would be fine. Or is this question about why this has been made undefined (i.e. what it buys the compiler)? – mafso Apr 05 '15 at 14:15
  • @mafso I am unable to get the difference between the sequential and uncertain order of evaluation. And also consequently, the rationale behind the decision. Was it simply a consequence of the definition of undefined behaviour due to the absence of a sequence point ? – Crazy-Fool Apr 05 '15 at 14:38
  • @Crazy-Fool, I'm not sure if it buys the compiler anything in this particular case, but in general the sequence point rules allow optimizations which wouldn't be possible otherwise. Even if it's not helpful for the compiler in this particular case, I see no compelling reason why the standard should allow for such constructs. – mafso Apr 05 '15 at 15:38
  • @mafso that clears it up a bit. Just one more question. I still can't get the fact that the construct would be fine had the evaluation been sequenced. Why so ? – Crazy-Fool Apr 06 '15 at 05:40
  • Well, not only compiler writers want some guarantees, there are also programmers using the compiler :) If there weren't any sequencing guarantees at all, e.g. `printf("hi\n"); printf("bye\n");` could print in either order -- not that useful. – mafso Apr 06 '15 at 09:44
  • Ah, and [this answer](http://stackoverflow.com/a/12540468/1741125) illustrates how such undefined aspects may help the compiler. – mafso Apr 06 '15 at 11:20
  • @mafso I think you got my question wrong. Here it has been pointed out by others that the sequence point error is due to the fact that the order of evaluation of the arguments of printf is not defined. I am confused by this. – Crazy-Fool Apr 06 '15 at 12:02
  • That's the case for every function call, not only `printf`. There are certain points which are specified to be sequence points, and function argument evaluation is not among them. – mafso Apr 06 '15 at 12:58
  • @mafso Would there be no sequence point error had the evaluation been guranteed to be sequential without any sequence point between the evaluations ? – Crazy-Fool Apr 06 '15 at 14:45
  • C99 has no definition of "sequenced" (what it is called in C11), there are only sequence points, so "guaranteed to be sequential without any sequence point between [...]" doesn't make sense by definition. If it was guaranteed to be sequenced, there would be a sequence point. E.g., `printf("foo") , printf("bar");` first prints "foo", then "bar", because the comma operator is a sequence point. `printf("foo") + printf("bar");`, on the other hand, may print either "foobar" or "barfoo", because addition is not a sequence point. Here the behavior is unspecified, either of the two outputs will occur. – mafso Apr 06 '15 at 16:14
  • ... If an object is modified and read (or modified) without an intervening sequence point, the behavior is _undefined_. – mafso Apr 06 '15 at 16:15
  • @mafso Okay thanks. I did not know about the absence of "sequenced" in C99. :) – Crazy-Fool Apr 06 '15 at 19:49

1 Answers1

5

It is true that you are changing the value of a only once but order of evaluation of arguments a and a++ is not guaranteed. Either a or a++ will be evaluated first resulting in undefined behavior.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • But how does that cause a sequence point error ? I am quite a beginner at C. Learning for last four months as a part of introduction to programming course in my university. So a more verbose explanation will probably be helpful :/ Thanks for helping. – Crazy-Fool Apr 04 '15 at 18:31
  • Downvoter care to explain? – haccks Dec 30 '19 at 06:40