"John Matthews" <[EMAIL PROTECTED]> wrote:
> "Paul Herring" <pauljherring@> wrote:
> > Asim Mian <asim_withlove@> wrote:
> > > In both the cases answer will be 10.....
> >
> > Not every time it won't. You're still invoking
> > undefined behaviour.
>
> ...so it might :-)
>
> I know what you mean, but of course unless you have
> a sadistic compiler, unfortunately it *is* likely you
> will get the same result every time you run it.
Depends. Here's two runs of the _same_ program running
on the _same_ implementation...
% type ubx.c
#include <stdio.h>
int foo(int *x) { return ++*x + ++*x; }
#define DUMP(expr) \
printf("%s: %d\n", #expr, (int) (expr))
int main(void)
{
int x, y;
x = 0; DUMP(y = ++x + ++x + ++x + ++x);
x = 0; DUMP(y = ++x + ++x + foo(&x));
x = 0; DUMP(y = ++x + foo(&x) + ++x);
x = 0; DUMP(y = foo(&x) + ++x + ++x);
return 0;
}
% gcc -ansi ubx.c -o ubx.exe
% gcc -ansi -O2 ubx.c -o ubx2.exe
% ubx
y = ++x + ++x + ++x + ++x: 10
y = ++x + ++x + foo(&x): 10
y = ++x + foo(&x) + ++x: 10
y = foo(&x) + ++x + ++x: 10
% ubx2
y = ++x + ++x + ++x + ++x: 11
y = ++x + ++x + foo(&x): 12
y = ++x + foo(&x) + ++x: 11
y = foo(&x) + ++x + ++x: 11
%
> It would be much better if it gave a random answer.
The above results aren't random, but they're not
exactly consistent either.
--
Peter