avr gcc allows case ranges?

Saw this over at AVRfreaks:

uint8_t foo(uint8_t i) 
{ 
        switch(i) { 
                case 0 ... 5:   return 1; // spaces around '...' are mandatory 
                case 10 ... 20: return 2; 
                default:        return 0; 
        } 
}

Is this considered legal C, or just a extra-standard enhancement? I loaded it to the end of the blink sketch and it compiled.

Lefty

It is an extension and not part of either the 'C' or 'C++' language standards.

Is this considered legal C

Probably not. I cannot find a reference that includes that syntax and Visual C spits out a long list of errors.

or just a extra-standard enhancement?

You may want to run a few tests before you call it an "enhancement". It's entirely possible that the output from the compiler does not execute quite the way you expect.

It's entirely possible that the output from the compiler does not execute quite the way you expect.

Well I didn't expect anything, just something I saw posted on AVRfreaks site. There are a lot of smart people over there, but I tend to not ask them questions if you know what I mean. :wink:

Anyway I think they said it's something unique to GNU gcc and applies to the avr gcc flavor also. So I assume that means it does work but is definitely not a portable feature across other C/C++ compilers nor is part of the 'official' standards for the language. Still it's neat and useful and I recall using ranges in case statements back in my old Turbo Pascal days running on CP/M systems. Boy am I old or what, and yes my lawn is off limits.

Lefty

There are a lot of smart people over there, but I tend to not ask them questions if you know what I mean

I do. They can be a surly bunch.

There are several neat and useful things I miss when using C(++) instead of Delphi (the TurboPascal successor). Ranges are definately one of those things. Sets, "const array of const", virtual methods in constructors, classes as objects, properties ... Hmm. I think I'll spend some time with that other compiler. ;D

I also seem to recall handling strings was less, lets say painful, then with C/C++? Or maybe that was in Basic language? I just try and avoid acsii all together as much as possible in my Arduino projects. ;D

Lefty

It's an official GCC extension to the C language:

--
The Rugged Motor Driver: two H-bridges, more power than an L298, fully protected

I also seem to recall handling strings was less, lets say painful, then with C/C++?

Absolutely! And string handling has only gotten better.

Or maybe that was in Basic language?

VisualBasic does well but the Delphi variation does some things even better.

I just try and avoid acsii all together as much as possible in my Arduino projects.

Ditto. I don't think I've done any string manipulation yet. I'm going to try to keep it that way.

I suspect "counted-strings" (the string implementation in TurboPascal) would work well on the Arduino. There are certainly some advantages. The big "disadvantage" is a 255 character limit. Counted-strings would fit well with what AlphaBeta was trying to do for a memory manager. Oh well. I've got other fish to fry.