1

I am working on a C project for a TI TMS320x DSP with the C2000 compiler. I tried to initialized a loop variable directly inside a for loop, but somehow I get a compiler error:

Code:

for (int TabCnt = 0; TabCnt < 10; TabCnt++)
{
    x++;
}

Error:

error #20: identifier "TabCnt" is undefined

I figure this might be a wrong compiler setting? If I declare the variable outside of the loop, it works perfectly.

4

2 回答 2

4

那是因为您使用的是仅支持 C89 的编译器。

这个语法:

for (int TabCnt = 0; TabCnt < 10; TabCnt++)

仅从 C99 开始有效。解决方案是在支持的情况下启用 C99,或者在块的开头声明变量,例如:

void foo()
{
    int x = 0;
    int TabCnt;
    for (TabCnt = 0; TabCnt < 10; TabCnt++)
    {
        x++;
    }
}
于 2015-03-24T08:59:23.230 回答
0
int TabCnt;

for(TabCnt = 0; TabCnt < 10; TabCnt++)

将解决您的问题,因为您的编译器似乎不支持 C99。

尝试编译,-std=c99因为您拥有的语法仅允许来自 C99

于 2015-03-24T09:01:15.123 回答