为什么编译器抱怨减去负数?

时间:2022-11-11 01:42:21
 #include <stdio.h>
 int main()
 {
  int a,b;
  a=-3--25;
  b=-3--(-3);
  printf("a=%d b=%d\n",a,b);
  return 0;
 }

In this programme, all I think is fine but still while building I got the error that [|5|error: expected ';' before numeric constant|.] I don't know what is wrong with this programme.

在这个程序中,我认为所有都很好,但仍然在构建时我得到了错误[| 5 |错误:预期';'在数字常量之前|。]我不知道这个程序有什么问题。

2 个解决方案

#1


5  

-- is the decrement operator. You can't apply the decrement operator to numeric constants, because it changes what it's applied to. That is

- 是减量运算符。您不能将减量运算符应用于数字常量,因为它会更改它应用的值。那是

--a;

changes the value of a. So --3 is invalid, you can't change a constant.

改变了a的值。所以--3无效,你不能改变常数。

If you want to subtract a negative number, you need a space between the minus operator - and the negation operator -, or () around the thing being negated:

如果你想减去一个负数,你需要在减号运算符 - 和否定运算符 - 之间留一个空格,或者在被否定的东西周围放一个空格:

a = -3 - -25;
b = -3 - -(-3);

Separately, note that -(-3) is a long-winded way to write 3.

另外,请注意 - ( - 3)是一种冗长的写作方式3。


In general, spaces and blank lines are not the enemy. Putting spaces around operators, and judicious use of blank lines, generally improves readability.

一般来说,空格和空白行不是敌人。在操作员周围放置空格,明智地使用空行,通常可以提高可读性。

#include<stdio.h>

int main()
{
    int a, b;

    a = -3 - -25;
    b = -3 - -(-3);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

#2


0  

Place some spaces between - sign and let the compiler know that you only want to use unary and binary -, not decrement operator --.

在 - 符号之间放置一些空格,让编译器知道你只想使用一元和二元 - 而不是递减运算符 - 。

a= -3 - -25;
b= -3 - -(-3);  

Since -- is decrement operator so a= -3--25; is not converted by compiler to a= -3 - -25;

因为 - 是递减运算符所以a = -3--25;编译器不会将其转换为= -3 - -25;

#1


5  

-- is the decrement operator. You can't apply the decrement operator to numeric constants, because it changes what it's applied to. That is

- 是减量运算符。您不能将减量运算符应用于数字常量,因为它会更改它应用的值。那是

--a;

changes the value of a. So --3 is invalid, you can't change a constant.

改变了a的值。所以--3无效,你不能改变常数。

If you want to subtract a negative number, you need a space between the minus operator - and the negation operator -, or () around the thing being negated:

如果你想减去一个负数,你需要在减号运算符 - 和否定运算符 - 之间留一个空格,或者在被否定的东西周围放一个空格:

a = -3 - -25;
b = -3 - -(-3);

Separately, note that -(-3) is a long-winded way to write 3.

另外,请注意 - ( - 3)是一种冗长的写作方式3。


In general, spaces and blank lines are not the enemy. Putting spaces around operators, and judicious use of blank lines, generally improves readability.

一般来说,空格和空白行不是敌人。在操作员周围放置空格,明智地使用空行,通常可以提高可读性。

#include<stdio.h>

int main()
{
    int a, b;

    a = -3 - -25;
    b = -3 - -(-3);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

#2


0  

Place some spaces between - sign and let the compiler know that you only want to use unary and binary -, not decrement operator --.

在 - 符号之间放置一些空格,让编译器知道你只想使用一元和二元 - 而不是递减运算符 - 。

a= -3 - -25;
b= -3 - -(-3);  

Since -- is decrement operator so a= -3--25; is not converted by compiler to a= -3 - -25;

因为 - 是递减运算符所以a = -3--25;编译器不会将其转换为= -3 - -25;