【发布时间】:2013-09-06 10:25:06
【问题描述】:
public class main {
public static void main(String[] args) {
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
System.out.printf("%d %d\n",x,y);
}
//Output : 56,93
#include<stdio.h>
void main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d ",x,y);
}
//Output : 57 94
根据运算符优先规则,我通过 Java 代码得到的任何输出都是正确的,但是在“C”代码中执行相同的输出时,它会将输出值增加 1。我使用的是 ubuntu 12.04 64 位操作系统。
【问题讨论】:
-
这是 C 和 C++ 中未定义的行为
-
C 代码不是有效的 C 代码。
-
应该
mainin c 返回 int 吗? -
@PeterLawrey “在 C 中,main 可以是 void 或 int”这不是我的 C99 标准副本在第 5.1.2.2.1:1 条中所说的。哪个子句告诉你
main()可以返回void? -
@PeterLawrey 根据c标准,编译和标准/非标准
main()之间可能存在差异,应该返回int类型。
标签: java c operators operator-precedence