Do you know what the output is?

Do you know what the output of this program is? Don't throw up! I know, it's a pretty disgusting programming.

Don't worry about posting spoilers, hopefully no one will look down from this post if they were planning on figuring it out. *Behaviour is undefined so actual results vary from compiler to compiler (and even compiler setting to compiler setting).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>

void FunctionTest(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n", c);
	printf("%d\n", d);
	printf("%d\n", e);
	printf("%d\n", f);
	printf("%d\n", g);
	printf("%d\n", h);
	printf("%d\n", i);
	printf("%d\n", j);
}

int main()
{
	int i = 10;
	
	FunctionTest(i--, --i, i--, --i, i--, --i, i--, --i, i--, --i);
}


INB4 "the only disgusting thing is printf".
Last edited on
This is simply undefined behavior. The sequencing of an i decrement is unspecified with regards to any other i decrement in this code.
closed account (1yR4jE8b)
Bro, do you even lift?!
Such massively erroneous programs are fun to feed to different compilers

10 8 8 6 6 4 4 2 2 0: XL C/C++ on IBM:
10 8 8 6 6 4 4 2 2 0: Clang++ on Linux:
10 8 8 6 6 4 4 2 2 0: Intel C++ on Linux:
10 0 8 0 6 0 4 0 2 0: GNU g++ on IBM:
10 0 8 0 6 0 4 0 2 0: Sun Studio C++ on Sun:
10 0 8 0 6 0 4 0 2 0: EkoPath C++ on LInux
10 0 9 0 8 0 7 0 6 0: Sun Studio C on Sun:
1 0 3 0 5 0 7 0 9 0: GNU g++ on Linux:
1 0 3 0 5 0 7 0 9 0: Visual Studio in Debug mode
5 5 6 5 7 5 8 5 9 5: Visual Studio in Release mode


Pretty patterns, but they are completely meaningless because a program with undefined behavior cannot be reasoned about.
Last edited on
closed account (1yR4jE8b)
Interesting how Visual Studio w/ Debug and g++ on Linux have the same output. Also interesting how Visual Studio in release mode is completely different than everything else, which have a few reoccuring patters. What about compiling with g++ at different optimization levels? -O0 -O{1,2,3} -Os ?
Wait, I got one more!

1 1 3 3 5 5 7 7 9 9 1979 K&R C on UNIX

(had to rewrite that function in K&R style for that one)
Oh, okay. I actually sat down and thought about it and VC Release agreed with me, which is what I used.
EDIT(Elaboration): I mean to say I was under the impression it was defined a certain way, and it happened to give that result. Must be that its implementations have rubbed off on me!

Pretty patterns indeed! Perhaps an interesting idea would be to think about what logic allowed the different compilers to arrive with those results?

Most important to me is the fact that on some compilers it almost appears as if the function parameter expressions are evaluated in left-to-right order. Do they actually do this, I thought it was defined as right-to-left?
Last edited on
Most important to me is the fact that on some compilers it almost appears as if the function parameter expressions are evaluated in left-to-right order. Do they actually do this, I thought it was defined as right-to-left?


It is defined as being undefined.
Last edited on
I thought it was defined as right-to-left?


It is unspecified, meaning the compiler can evaluate them in any order it sees fit.
I think the OP is confusing the evaluation order (which is undefined) with the order in which the parameters are placed on the stack.
guestgulkan wrote:
I think the OP is confusing the evaluation order (which is undefined) with the order in which the parameters are placed on the stack.


No, not at all. I see where you're coming from but I am fully aware that you could evaluate the expressions in a different order.

I was simply told that the expressions themselves were evaluated in RTL order.

Well, this has been educational to me the most, it seems. Thanks to all repliers!
darkestfright wrote:
Bro, do you even lift?!


Unspecified.
Topic archived. No new replies allowed.