gpt4 book ai didi

c++ - 为什么我可以在函数的返回语句后输入任意多的分号?

转载 作者:行者123 更新时间:2023-11-28 00:53:15 26 4
gpt4 key购买 nike

我发现在函数返回语句中我可以做到这一点。

int f() {
return 1;;;;;;;;;;;;;;;;;;;;;;;; // ...;;;
}

我可以根据需要添加任意数量的分号,并且上述操作不会导致任何编译器或运行时错误。这让来自 C++ 这样严格的语言的我感到惊讶。有人可以解释为什么我能做到这一点吗?但是,我无法在任何其他情况下执行此操作。

int x = 1;;;; // error

那么有人能告诉我为什么我能做这样的事情吗?

最佳答案

引用自 K&R,表达式语句具有乘积 expression-statement: expression;

If the expression is missing, the construction is called a null statement; it is often used to supply an empty body to an iteration statement to place a label.

这是一种有效的语言结构,因此您的编译器不会有任何提示。

关于c++ - 为什么我可以在函数的返回语句后输入任意多的分号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12878242/

26 4 0