for (uint8_t i=0; i = 0x28; i++) >> outside C99 mode?

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I don't understand the compiler error, can you help explain?

...
for (uint8_t i=0; i = 0x28; i++)
...

Quote:

main.c: In function `main':
main.c:170: error: 'for' loop initial declaration used outside C99 mode
main.c:170: warning: suggest parentheses around assignment used as truth value

thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

within the for loop, your test statement is an assignment, not a test.
Did you really men to have ;i = 0x28; ?

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this from a C or C++ program?

Also, you have a declaration in the initialization of the for loop which is not legal in C.

Tom

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
main.c:170: error: 'for' loop initial declaration used outside C99 mode

The embedded declaration is a C99 feature. Try building with "-std=gnu99" or some such.
Quote:
main.c:170: warning: suggest parentheses around assignment used as truth value

This message is slightly off-target, but it's trying to tell you that, as Tom says,
your second clause (termination test) is Always true. Did you mean "i < 0x28"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes, it should be ;i==0x28; //thanks

In C99 the variable can be declared in the loop and its scope is limited to the loop, If I am reading my reference book correctly!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As suggested by other posters, this is an issue with the declaration of the loop variable.

Classic C (aka C89) requires all variables to be declared at the top of a block; declaring within the loop statement is illegal. On the other hand, C++ allows declaration like this, and so does the latest C standard (C99).

GCC is trying to be helpful in suggesting a reference to C99. This is likely more confusing than helpful however, I've run into this question from a numbe of people.

GCC will enable C++ compilation if your source file ends in ".cpp" but defaults to C89 if you use a suffix of ".c". If you really meant to use C++ style declarations then you should rename your source file.

BTW the loop terminating condition is likely not what you expect, you might want to check it as it will pretty much assure your loop never executes. You probably meant something like this:

...
for (uint8_t i=0; i <= 0x28; i++)
...

- Bob Cook

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
yes, it should be ;i==0x28;

This test will (always) be false the first time through, so your loop will never run.
Quote:
In C99 the variable can be declared in the loop and its scope is limited to the loop

But as far as the compiler is concerned you're not using (building) C99.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR-GCC defaults to C89 if no special arguments are passed to it.

There's a command line switch you have to provide to choose between K&R, C89, C89 + GNU extensions, C99, or C99 + GNU extensions.

The template makefile that comes with WinAVR sets the compiler to use the GNU99 standard by default. The makefile template that is used by AVR Studio doesn't specify any standard, so C89 is chosen by default.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IIRC, it defaults to C89 + GNU extensions. And, while the ability to define
a variable anywhere was already part of the GNU extensions, the ability
to declare it *in the for statement itself* has been explicitly disabled there.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	
	for (uint8_t x=0x00; x <= 0x28; x++) {
	dw1(0x00, DWAI);
	}

now compiles with

thank you

Quote:
CPFLAGS = -g -Os -Wall -Wstrict-prototypes -I -Wa,-ahlms=$(<:.c=.lst) -std=c99