unexpected printout for int

Feb 27, 2017 at 9:31am
Here is my bash session:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
agauld@ubuntu:~/src/cc/tour$ cat testinc.cc
#include <iostream>
#include <stdio.h>

using namespace std;

int main(){
    int n = 0;

    cout << "n: " << n << ++n << n++ << n << "\n";

    n = 0;
    printf("n: %d%d%d%d\n",n,++n,n++,n);

}
agauld@ubuntu:~/src/cc/tour$ make testinc
make: `testinc' is up to date.
agauld@ubuntu:~/src/cc/tour$ ./testinc
n: 2202
n: 2202 


I was trying to compare the behaviour of ++n and n++
and how that worked within a cout context.

I expected, or hoped for, 0112.
What I got - 2202 - surprised me.
Can someone please explain what happened?

Last edited on Feb 27, 2017 at 9:33am
Feb 27, 2017 at 9:36am
1
2
cout << "n: " << n << ++n << n++ << n << "\n";
printf("n: %d%d%d%d\n",n,++n,n++,n);


Mixing prefix and postfix increments in the same line like that is considered undefined behavior. Other compilers may not act the same as yours.
Last edited on Feb 27, 2017 at 9:38am
Feb 27, 2017 at 10:13am
Thanks, that was actually why I did it: to see how they were treated
when on a single line. But I assumed I'd either get the result I
expected or an error, I hadn't considered "undefined behaviour" as an
outcome! :-)

Thanks again.
Feb 27, 2017 at 10:23am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstdio>

int main() {

    int n = 0;

    // undefined behaviour pre-C++17
    // well defined behaviour in C++17; prints n: 0112
    // C++17: in x << y, value computation and side-effect of x
    //                   is sequenced before the value computation and side-effect of y
    std::cout << "n: " << n << ++n << n++ << n << '\n' ;

    n = 0;

    // undefined behaviour pre-C++17
    // unspecified behaviour in C++17
    // C++17: in fn(x,y), value computations and side effects of x and y
    //                    are indeterminately sequenced with respect to each other
    std::printf( "n: %d%d%d%d\n", n, ++n, n++, n );
}
Feb 27, 2017 at 10:27am
@JLBorges
Sorry, what is unspecified behaviour and what is the difference between undefined behaviour and unspecified behaviour?
Feb 27, 2017 at 10:43am
From the horse's mouth:
undefined behavior
behavior for which this International Standard imposes no requirements
[Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. Permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed. Evaluation of a constant expression never exhibits behavior explicitly specified as undefined. —end note ]

unspecified behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation
[Note: The implementation is not required to document which behavior occurs. The range of possible behaviors is usually delineated by this International Standard. —end note ]


The difference between unspecified behaviour and implementation-defined behaviour is that the latter must be documented behaviour.
implementation-defined behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents
Topic archived. No new replies allowed.