2

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
  • 25
  • 3

3 Answers3

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
  • 18,572
  • 3
  • 31
  • 36
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
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,784
  • 4
  • 24
  • 36
0

For anyone looking how to enable c99 in Code Composer Studio (the editor used to develop embedded C for Texas Instruments microcontrollers), go to Project -> Properties and then in the left hand side go to Build -> C2000 compiler -> Advanced Options -> Language options. You can select c99 in the drop-down menu of the "C Dialect" parameter. enter image description here

Note: I am using CCS v11.2.0.00007