6/2(1+2)返回错误2不是函数

时间:2020-12-12 23:14:03

The following statement is generating a compile-time error.

下面的语句生成编译时错误。

 int a=6/2(1+2);

Can someone please explain why the compiler generates an error.

有人能解释一下为什么编译器会产生错误吗?

4 个解决方案

#1


11  

You're missing a mathematical sign such as +, -, *, /.

你漏掉了一个数学符号,比如+,-,*,/。

You probably want 6/(2*(1+2)) or (6/2)*(1+2).

你可能想要6/(2*(1+2))或(6/2)*(1+2)。

If you leave the sign out, C interprets it as a function call just like usual functions printf("stuff") (indicated via opening parentheses without mathematical operator). So it thinks 2(1+2) calls the function 2 with argument 1+2.

如果您省略符号,C将它解释为函数调用,就像通常的函数printf(“stuff”)(通过没有数学运算符的圆括号表示)一样。它认为2(1+2)用参数1+2调用函数2。

#2


2  

You can't skip the multiplication operator. Try int a=6/2*(1+2);

你不能跳过乘法运算符。试着int = 6/2 *(1 + 2);

#3


1  

You have to do

你要做的

int a = 6/2*(1+2);

Otherwise it tries to interpret 2 as a function, like int a = 2(argument);

否则,它试图将2解释为函数,如int a = 2(参数);

#4


0  

There is no operation between 2 and (1 + 2). If you wanted to multiply, you have to let C know: the programming syntax is usually strict with this stuff.

2和(1 + 2)之间没有运算,如果你想要相乘,你必须让C知道:编程语法通常对这些东西很严格。

The correct syntax:

正确的语法:

int a = 6 / 2 * (1 + 2);

#1


11  

You're missing a mathematical sign such as +, -, *, /.

你漏掉了一个数学符号,比如+,-,*,/。

You probably want 6/(2*(1+2)) or (6/2)*(1+2).

你可能想要6/(2*(1+2))或(6/2)*(1+2)。

If you leave the sign out, C interprets it as a function call just like usual functions printf("stuff") (indicated via opening parentheses without mathematical operator). So it thinks 2(1+2) calls the function 2 with argument 1+2.

如果您省略符号,C将它解释为函数调用,就像通常的函数printf(“stuff”)(通过没有数学运算符的圆括号表示)一样。它认为2(1+2)用参数1+2调用函数2。

#2


2  

You can't skip the multiplication operator. Try int a=6/2*(1+2);

你不能跳过乘法运算符。试着int = 6/2 *(1 + 2);

#3


1  

You have to do

你要做的

int a = 6/2*(1+2);

Otherwise it tries to interpret 2 as a function, like int a = 2(argument);

否则,它试图将2解释为函数,如int a = 2(参数);

#4


0  

There is no operation between 2 and (1 + 2). If you wanted to multiply, you have to let C know: the programming syntax is usually strict with this stuff.

2和(1 + 2)之间没有运算,如果你想要相乘,你必须让C知道:编程语法通常对这些东西很严格。

The correct syntax:

正确的语法:

int a = 6 / 2 * (1 + 2);