cfunctionfactorial

Is this function to compute the factorial in C correct?


I have been reading the book "Hacking - the art of exploitation" (2nd edition, No Starch Press) by Jon Erickson. On page 17, I have stumbled over some code in C of a function that should compute the factorial of a number. I have some programming skills in Python but I am a beginner in C. The code is:

int factorial(int x)
{
  int i;
  for(i=1; i < x; i++)
    x *= i;
  return x;
}

int a=5, b;
b = factorial(a);

It is written in the book that the variable b will contain 120, since the factorial function will be called with the argument of 5 and will return 120.

Is this correct? As far as I know, x is assigned a new number on every iteration, so the condition i < x is always true. This looks like an infinite for loop to me, but I may be wrong. I have checked the errata on the publisher's website, but I couldn't find information that cleared my question.

If I have made a mistake, can somebody explain me what is wrong? I attach an excerpt of the book for reference.

excerpt from the book


Solution

  • You're correct in your observation. The function as written will result in an infinite loop. This is because the condition i < x will always be true as x is being increased in each iteration of the loop (x *= i).

    Based on your implementation, I will create another variable to hold the result, like so:

    int factorial(int x)
    {
       int i;
       int result = 1;
       for (i = 1; i <= x; i++)
          result *= i;
       return result;
    }
    int a=5, b;
    b = factorial(a); // expected result = 120