我的 C 书中的一个神奇数字 127
可能的重复:
什么限制了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该数字来自 ISO C 标准,ISO/IEC 9899 :1999:
这些是符合标准的 C 编译器必须能够处理的最小值。
This number comes from the ISO C standard, ISO/IEC 9899:1999:
These are the minimum values a conforming C compiler must be able to handle.
请参阅第 5.2.4.1 节中的 C99 标准转换限制,第 32 页。C99
标准定义了至少 127 级的块嵌套。 AFAIK 每个编译器实现都可以自由地提供比这更高的值。
块基本上就是 C 函数定义中大括号内的内容。块的级别是从外部块向内部块计数定义的。请参阅:
我真的不知道函数的主体实际上是级别 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:
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.
我猜想这与有符号 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.