1. Home
  2. Computing & Technology
  3. C / C++ / C#
David Bolton
David's C / C++ / C# Blog

By David Bolton, About.com Guide to C / C++ / C#

C/C++ Programming Puzzle

Tuesday April 8, 2008
It's been a while, so just to refresh you; what will this output?

#include

int fx(int * p) {
   return (*p += 1) ;
}
int main()
{
   int v=1;
   printf("%d %d %d",v++,fx(&v),v) ;
  return 0;
}

Answer in a couple of days...

Comments
April 8, 2008 at 8:49 pm
(1) Philip Snelgrove says:

It SHOULD output

1 3 3

but if it is done in VS it will probably output

1 2 2

now to try it…

April 8, 2008 at 9:42 pm
(2) someone says:

The about behavior is unknowable for ceratin by the standard I believe. The order of function argument evaluation is indeterminate and compiler specific. So it could output, 1 3 3, 2 2 1. Also unless rvo is applied or an r-value references being used as will be introduced by the new standard the returned int is a tempoarary.

April 8, 2008 at 10:28 pm
(3) qianyanjiang says:

it’s a classic problem related to the argument ‘order’. it seams that both the platform and compiler matters…

2 2 2 (X86_64/linux/gcc 3.4.5)
2 2 1 (X86/linux/gcc 2.96)
2 2 1 (X86/windows/vc6.0)

I find another interesting example:
#include
int
main (int argc, char **argv)
{
long i = 0;

printf (”%d %d %d %d %d %d %d %d %d %d\n”, i++, i++, i++, i++, i++, i++, i++, i++, i++, i++);
return 0;
}

x86_64(linux/gcc3.4) output:
4 3 2 1 0 9 8 7 6 5
x86(linux/gcc2.9) output:
9 8 7 6 5 4 3 2 1 0
x86(windows/vc6.0) output:
0 0 0 0 0 0 0 0 0 0

April 9, 2008 at 12:29 pm
(4) Mike says:

The result is undefined. When a variable is both read and written to more than once in the same sequence point the C standard says the behaviour is undefined.

April 9, 2008 at 1:52 pm
(5) André Prata says:

ANSI C defines that the expressions are evaluated from right to left.
(think assembly… the caller function is supposed to push from right to left…)

So the output should be: 2 2 1.

Mind that v++ increments v after the push…

April 10, 2008 at 5:48 am
(6) Sandeep says:

It is compiler specific.
In Solaris it should give the output as
2 2 3
But in VS it should give the o/p as
2 2 2

April 10, 2008 at 10:08 am
(7) xtremezone says:

GCC/gcc(4.1.1) and MinGW/gcc(3.4.5) both give…

2 2 2

April 11, 2008 at 2:13 am
(8) Neha says:

It should be 1 3 3

April 15, 2008 at 3:21 pm
(9) danny says:

1 3 2

April 15, 2008 at 3:23 pm
(10) danny says:

… 1 3 3

April 15, 2008 at 3:25 pm
(11) danny says:

…oops, calculated the mistake as if the dereferenced value was only inside the scope of the function.

April 17, 2008 at 2:22 am
(12) J. Shaffer says:

There is no correct output.

The ANSI C standard doesn’t guarantee the order in which parameters are evaluated when calling a function.

When values are mutated in the argument expressions and used more than once, the answer you get on your compiler is whatever was convenient on the architecture, calling convention and the compiler’s grammar.

November 3, 2009 at 2:44 pm
(13) salil tiwari says:

the output of this program in gcc compiler is as:
2 2 1

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.