Possible Duplicate:
What limits the number of nested loops in c?
Hello.
When I read my C book, it says
Nesting for-Loop in C can continue even further up to 127 levels!
How does 127 come from?
My book doesn’t mention about this. Just like a magic number to me.
[update]
int main()
{
int number, n, triangularNumber, counter;
triangularNumber = 0;
for (counter = 1; counter <= 5; ++counter){
printf("What triangular number do you want? \n");
// using a routine called scanf
scanf("%i", &number);
triangularNumber = 0;
for (n =1 ; n <= number; ++n)
triangularNumber += n;
printf("Triangular number %i is %i\n", number, triangularNumber);
}
return 0;
}
See the C99 standard in section 5.2.4.1 Translation limits, page 32.
The C99 standard defines a minimum of 127 level of nesting for blocks. AFAIK each compiler implementation is free to provide a higher value than this.
A block is basically what goes inside curly braces in C’s function definitions. And the level of a block is defined counting from the outside block towards the inner block. See:
I really don’t know if the body of the function is actually level 1 or level 0 but this was just for you to get the idea of how it works.
This minimum value is so the standard guarantees that programs that follow this limitation would be able to compile in different implementations of C language compilers without modification.
Note that code with too deep levels can lead to excessively large functions which is a code smell.