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.

2
  • 1
    You need to enforce c99 mode in the compiler setting. Commented Mar 24, 2015 at 8:58
  • Possible duplicate of C: for loop int initial declaration Commented Jul 24, 2018 at 19:23

3 Answers 3

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++;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I thought it might be something like that. Thanks!
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.