我的 C 书中的一个神奇数字 127

发布于 2024-10-15 04:48:03 字数 886 浏览 2 评论 0原文

可能的重复:
什么限制了 c 中的嵌套循环数量?

你好。

当我读我的 C 书时,它说

C 中的 for 循环嵌套甚至可以继续到 127 层!

127是怎么来的?

我的书里没有提到这一点。对我来说就像一个神奇的数字。

[更新]

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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情何以堪。 2024-10-22 04:48:03

该数字来自 ISO C 标准,ISO/IEC 9899 :1999:

5.2.4.1 翻译限制

实施应能够
翻译并执行至少一项
程序至少包含一个
以下每一项的实例
限制:

  • 127 级块嵌套
  • 条件包含的 63 层嵌套
  • 12 个指针、数组和函数声明符(任意组合)
    修改算术、结构、
    联合,或不完整的类型
    声明
  • 完整声明符内带括号的声明符有 63 层嵌套
  • 完整表达式内带括号的表达式有 63 层嵌套
  • 内部标识符或宏名称中的 63 个重要初始字符
    (每个通用角色名称或
    扩展源字符是
    被视为单个字符)
  • 外部标识符中的 31 个重要初始字符(每个通用字符
    指定短字符的名称
    0000FFFF 或更小的标识符是
    考虑 6 个字符,每个
    通用字符名称指定
    00010000 或以上的短标识符
    被认为是 10 个字符,并且每个
    扩展源字符是
    被认为是相同数量的
    字符作为对应的
    通用角色名称(如果有)
  • 一个翻译单元中有 4095 个外部标识符
  • 在一个块中声明了 511 个具有块作用域的标识符
  • 在一个宏标识符中同时定义 4095 个宏标识符
    预处理翻译单元
  • 一个函数定义中有 127 个参数
  • 一次函数调用中有 127 个参数
  • 一个宏定义中有 127 个参数
  • 一次宏调用中有 127 个参数
  • 逻辑源代码行中包含 4095 个字符
  • 字符串文字或宽字符串文字中的 4095 个字符
    (连接后)
  • 对象中 65535 字节(仅在托管环境中)
  • #included 文件有 15 个嵌套级别
  • switch 语句有 1023 个 case 标签(不包括任何
    嵌套 switch 语句)
  • 单个结构或联合中有 1023 个成员
  • 单个枚举中有 1023 个枚举常量
  • 单个文件中有 63 层嵌套结构或联合定义
    结构声明列表

这些是符合标准的 C 编译器必须能够处理的最小值

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.

音盲 2024-10-22 04:48:03

请参阅第 5.2.4.1 节中的 C99 标准转换限制,第 32 页。C99

标准定义了至少 127 级的块嵌套。 AFAIK 每个编译器实现都可以自由地提供比这更高的值。

块基本上就是 C 函数定义中大括号内的内容。块的级别是从外部块向内部块计数定义的。请参阅:

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 */
                ...
            }
        }
    }
}

我真的不知道函数的主体实际上是级别 1 还是级别 0,但这只是为了让您了解它是如何工作的。

这个最小值是标准保证遵循此限制的程序能够在 C 语言编译器的不同实现中进行编译而无需修改。

请注意,级别太深的代码可能会导致函数过大,这是 代码气味

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.

贩梦商人 2024-10-22 04:48:03

我猜想这与有符号 8 位整数的大小有关。有符号 8 位整数可以取的最大值是 127。但是,我确信 for 循环的嵌套深度取决于所使用的特定编译器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文