9

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;
}
Community
  • 1
  • 1
Nano HE
  • 9,109
  • 31
  • 97
  • 137
  • 2
    Is there any code associated with it? Maybe they were using a `signed char` as a loop counter (which goes up to 127 in 8-bit `char` representations). – wkl Jan 31 '11 at 03:36
  • I attached the sample code above. – Nano HE Jan 31 '11 at 03:38
  • @Triniada, The source code used for For-Loop Nesting introduction in my book. – Nano HE Jan 31 '11 at 05:00
  • The standard had to define *some* lower bound on the level of nesting a compiler must support. The choice of 127 (which is deeper than any reasonable program would use) is intended, I think, to make it easier for compiler writers to support *arbitrarily deep* nesting than to impose a hard limit. Typically the actual limit is imposed by the availability of memory and other resources at compile time. – Keith Thompson Dec 11 '13 at 17:09

3 Answers3

13

This number comes from the ISO C standard, ISO/IEC 9899:1999:

5.2.4.1 Translation limits

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:

  • 127 nesting levels of blocks
  • 63 nesting levels of conditional inclusion
  • 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or incomplete type in a declaration
  • 63 nesting levels of parenthesized declarators within a full declarator
  • 63 nesting levels of parenthesized expressions within a full expression
  • 63 significant initial characters in an internal identifier or a macro name (each universal character name or extended source character is considered a single character)
  • 31 significant initial characters in an external identifier (each universal character name specifying a short identifier of 0000FFFF or less is considered 6 characters, each universal character name specifying a short identifier of 00010000 or more is considered 10 characters, and each extended source character is considered the same number of characters as the corresponding universal character name, if any)
  • 4095 external identifiers in one translation unit
  • 511 identifiers with block scope declared in one block
  • 4095 macro identifiers simultaneously defined in one preprocessing translation unit
  • 127 parameters in one function definition
  • 127 arguments in one function call
  • 127 parameters in one macro definition
  • 127 arguments in one macro invocation
  • 4095 characters in a logical source line
  • 4095 characters in a character string literal or wide string literal (after concatenation)
  • 65535 bytes in an object (in a hosted environment only)
  • 15 nesting levels for #included files
  • 1023 case labels for a switch statement (excluding those for any nested switch statements)
  • 1023 members in a single structure or union
  • 1023 enumeration constants in a single enumeration
  • 63 levels of nested structure or union definitions in a single struct-declaration-list

These are the minimum values a conforming C compiler must be able to handle.

makes
  • 6,438
  • 3
  • 40
  • 58
  • 3
    +1, I learned something new today. I've come across a lot of strange code, but never nesting to the extreme that such a limit would be tested. Now I'm curious to look at the tests that gcc ships with just to see how badly they abuse it :) – Tim Post Jan 31 '11 at 04:07
  • 2
    I like how the "unlucky 13th" level of pointer indirection or functional abstraction is allowed to fail. – Potatoswatter Jan 31 '11 at 04:29
7

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:

void myFunction() {
    int x = 2;
    /* level 1 block */
    while(1) {
        /* level 2 */
        if (x > 1) {
            /* level 3 */
            ...
        } else {
            int i;
            /* also level 3 */
            for (i = 0; i < x; ++i) {
                /* level 4 */
                ...
            }
        }
    }
}

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.

Trinidad
  • 2,756
  • 2
  • 25
  • 43
0

I would guess it has to do with the size of a signed 8 bit integer. The largest value a signed 8 bit integer can take is 127. However, I am sure how deep you can nest for loops depends on the specific compiler used.

Alex W
  • 142
  • 3