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.

Simon
  • 15
  • 3

2 Answers2

4

That's because you are using a compiler that supports only C89.

This syntax:

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

is only valid since C99. The solution is either enable C99 if supported, or declare variables in the beginning of a block, e.g:

void foo()
{
    int x = 0;
    int TabCnt;
    for (TabCnt = 0; TabCnt < 10; TabCnt++)
    {
        x++;
    }
}
Supr
  • 17,742
  • 3
  • 29
  • 35
Yu Hao
  • 115,525
  • 42
  • 225
  • 281
0
int TabCnt;

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

will solve your problem as it seems your compiler doesn't support C99.

Try compiling with -std=c99 since the syntax you have is allowed only from C99

Gopi
  • 19,679
  • 4
  • 22
  • 35