我是 C 新手,有点困惑,我有一些代码,在声明时将 += 运算符与变量一起使用,但它给了我这个运算符的错误,但反向使用它时工作正常,即 =+,请解释为什么? **这是代码**
int i = 0;
int g = 99;
do
{
int f += i; //Here += is throwing error and =+ is working fine why?
printf("%-6s = %d ","This is",f);
i++;
} while(i<10);
Error as
ff.c:16:11: error: invalid '+=' at end of declaration; did you mean '='?
int f += i;
^~
=
1 error generated.
它是这样工作的
int i = 0;
int g = 99;
do
{
int f =+ i; //Here += is `your text throwing error and =+ is working fine why?
printf("%-6s = %d ","This is",f);
i++;
} while(i<10);
定义
int f =+ 1;
确实是一样的
int f = +1;
也就是说,您正在使用值
f
初始化 +1
。
在定义时初始化变量时,只能初始化它,不能修改它。
+=
是C
中的特殊运算符,它与=+
完全不同。
f += x
将 x
添加到 f
并将其存储在 f
中。它等同于:
f = f + x;
当您声明
f
时,您无法向其中添加任何内容,因为此时它不存在 - 因此会出现错误。
f=+1
将 f
与 1
指定为 +1 == 1
。