0

This (a() I mean) returns a random number with a function defined as :

int a (void) {
    for (int e = 0; e < 10; e++);
}

And this is a factorial function I've created, but no idea why it is returning a value (and it works !) :

long int factorial (int n) {
    for (long int s = 1; s *= n, --n;);
}

Is it some C weird feature ? And what's the rule to know what it will return ?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
JNa0
  • 131
  • 7
  • 4
    Turn compiler warnings on and lo and behold...the compiler complains! – Paul Ogilvie Oct 15 '18 at 14:26
  • 2
    The rule is on Intel: the `eax` register generally holds the int return value. So the value that was put there is what the caller assumes is returned. since you don't know what the compiler generated for code, you can't know what the compiler put in eax and you have _Accidental Correct Bahavor_, a subset of _Undefined Behavior_. – Paul Ogilvie Oct 15 '18 at 14:27
  • 2
    This is undefined behavior according to the standard, so there are no rules you could follow to determine what the posted code does. – Mat Oct 15 '18 at 14:31

0 Answers0