为什么下面的代码不会产生编译错误?

发布于 2024-12-23 03:11:45 字数 158 浏览 2 评论 0原文

我正在使用 VS2005 编译器,我希望以下代码给出编译错误。

int a=1, b= 2, c=3;
a = (b,c);

赋值后的值为 3。根据我的理解,它应该给出编译错误。

我很高兴知道这背后是否有任何正当理由。

I am using VS2005 compiler and I am expecting following code to give compilation error.

int a=1, b= 2, c=3;
a = (b,c);

value of a after assignment is 3. As per my understanding it should give compilation error.

I would be happy to know if there is any valid reason behind this.

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

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

发布评论

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

评论(5

拥抱没勇气 2024-12-30 03:11:45

您的代码中唯一的错误是您忘记了第二行末尾的分号,但我认为这是撰写问题时的印刷错误,而不是问题的实际主旨。

我认为您所显示的代码没有理由产生编译错误。它对我来说编译得很好,事实上,a 的值是 3。

参见你自己:下面的代码返回 3:

int main()
{
    int a=1, b= 2, c=3;
    a = (b,c);

    return a;
}

诀窍是你使用 逗号运算符,它评估其第一个操作数并然后丢弃结果,然后计算第二个操作数并返回其值。

但是,正如 Charles Bailey 指出的那样,您必须将问题中显示的代码包装在函数内,否则您将得到任何编译器中的编译错误。 C++ 不允许在函数之外使用赋值语句。

The only error in your code is that you forgot the semicolon at the end of the second line, but I assume that was a typographical error in composing your question, rather than the actual thrust of your question.

I see no reason why the code you've shown should produce a compilation error. It compiles just fine for me, and the value of a is, in fact, 3.

See for yourself: the following code returns 3:

int main()
{
    int a=1, b= 2, c=3;
    a = (b,c);

    return a;
}

The trick is your use of the comma operator, which evaluates its first operand and then discards the result, and then evaluates the second operand and returns its value.

However, as Charles Bailey notes, you have to wrap the code shown in the question inside of a function, otherwise you will get compilation errors in any compiler. C++ doesn't allow assignment statements outside of functions.

佞臣 2024-12-30 03:11:45

您在 C++ 中使用逗号运算符,它并不常用。其工作原理如下:

<expression1>, <expression2>

它评估 并丢弃结果,然后评估 并将返回的结果作为整个表达式的值。

You are using the comma operator in C++, it is not commonly used. This works as follows

<expression1>, <expression2>

It evaluates <expression1> and discards the results and then evaluates <expression2> and takes the result of that is returned as the value of the whole expression.

梦里兽 2024-12-30 03:11:45

这段代码没有错误。为什么你认为应该有编译错误?这里只是一个 逗号运算符,它评估其所有参数,但返回最右边的一个:在本例中为 3。

There is no error in this piece of code. Why do you think there should be a compilation error? All here is is a comma operator which evaluates all its parameters, but returns the rightmost one: in this case 3.

乖不如嘢 2024-12-30 03:11:45

引用http://en.wikipedia.org/wiki/Comma_o​​perator

“在 C 和 C++ 编程语言中,逗号运算符(由标记 , 表示)是一个二元运算符,它计算第一个操作数并丢弃结果,然后计算第二个操作数并返回该值(和类型)逗号运算符的优先级是所有 C 运算符中最低的,并且充当序列点。”

To quote http://en.wikipedia.org/wiki/Comma_operator:

"In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point."

忆沫 2024-12-30 03:11:45

我刚刚在 VS2005 上对此进行了测试,并且按照预期得到了编译错误。

Compiling...
main.cpp
d:\dev\work\comptest\main.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\dev\work\comptest\main.cpp(2) : error C2374: 'a' : redefinition; multiple initialization
        d:\dev\work\comptest\main.cpp(1) : see declaration of 'a'

声明行很好,但正如预期的那样,赋值语句在函数外部无效。编译器似乎将其解释为尝试使用默认的 int 类型重新初始化 a,这两种类型都不合法。

I've just tested this on VS2005 and I get compilation errors as expected.

Compiling...
main.cpp
d:\dev\work\comptest\main.cpp(2) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\dev\work\comptest\main.cpp(2) : error C2374: 'a' : redefinition; multiple initialization
        d:\dev\work\comptest\main.cpp(1) : see declaration of 'a'

The declaration line is fine, but as expected the assignment statement is not valid outside of a function. The compiler appears to interpret it as an attempt to re-initialize a with a default int type, neither of which is legal.

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