What's your output?

Run this code and tell what your output is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void PrintOrder(unsigned long a, unsigned long b, unsigned long c, unsigned long d)
{
	cout << a << b << c << d << endl;
}

unsigned long Staticount()
{
	static unsigned long v = 0;
	return(v++);
}

int main()
{
	PrintOrder(Staticount(), Staticount(), Staticount(), Staticount());
	cin.sync();
	cin.ignore();
}


My output is 3210, my compiler in VC++2008 Express evaluates the parameters from right to left, which was the opposite of what I thought. I'm curious as to the order your compiler(s) evaluate(s) parameters, as the order is undefined. This tricked me up once, because I assumed the parameters to a function call would be evaluated from left to right, I spent hours debugging this until someone finally pointed it out to me >_<
I also get 3210 on VC++2010 express, but as you mentioned, the output is undefined.
closed account (S6k9GNh0)
If I'm not mistaken, it's naturally read from right to left because of the nature of the underlying assembly. I'm not to avid in how Assembly works (well, I understand ASM, but not the concepts that surround it) so I really can't be certain about it.
Last edited on
A teacher once told me it's done this way to implement variadic functions. If the parameters are pushed right to left, the stack will look like this:
<variadic parameters>
3rd parameter
2nd parameter
1st parameter
Then, by passing a pointer to the third parameter and since the beginning of the stack frame is always known, it's easy to determine how many parameters were passed.
closed account (S6k9GNh0)
^ this. This is what I was talking about.
Topic archived. No new replies allowed.