【发布时间】:2020-10-20 03:20:16
【问题描述】:
看着wikipedia 它说:
a -= b;
与
相同a = a - b;
但是当我在我的 C 程序中尝试这个时,我得到了以下错误:
"error: redefinition of 'a'".
这是我的程序:
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 10;
int a -= 5;
printf("a has a value of %d\n", a);
return 0;
}
我收到以下错误:
my_prog.c:6:6: error: redefinition of 'a' int a -= 5; ^ my_prog.c:5:6: note: previous definition is here int a = 10; ^ my_prog.c:6:8: error: invalid '-=' at end of declaration; did you mean >'='? int a -= 5; ^~
我在 Mac 上使用 clang。
【问题讨论】:
-
缺少分号。
-
在上下文方面我已经添加了我的代码,我认为标题很清楚。
-
int a....是一个定义。但是你已经定义了它。你想要一个表达式。删除int。 -
标题对 R 程序员来说很清楚,因为在 R 中,赋值 是 一个定义。在 C 中,赋值语句会更改现有变量的值。它不会创建新变量,也不能用于更改现有变量的类型。
标签: c redefinition compound-assignment