0

I am desperately looking for a way to easily calculate the number of all possible execution paths in a C function.

For example, for the following function I would expect to get a result of 3 (if there is a chance based on the values that 'i' gets to enter any of the 'if' statements)

void test(void)
{
   if (i>0)
     x = x + 1;
   else if (i>10)
     x = x + 2;
   else
     x = x + 3;
}
limp
  • 889
  • 2
  • 14
  • 22

1 Answers1

-1

Use comma operator as

int test(void)
{
   int ways = 0;
   if (++ways, i>0)
     x = x + 1;
   else if (++ways, i>10)
     x = x + 2;
   else
   {
     x = x + 3;
     ++ways;
   }
   return ways;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • One of the problems I see with your method is that it doesn’t work if we have nested conditional statements. For example, if we put another if statement inside the first if, it will give a result of 2 instead of the correct 4. – limp Feb 04 '15 at 20:08
  • @limp; You need to put `++ways` in each of your conditional expression. – haccks Feb 04 '15 at 20:12