Does CV allow C99 variable declarations?

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

Evidently as time marches on, I fall further behind the Best Practices. I notice that everyone likes to declare variables at the point of use, like in for loops. This was introduced in C99 I think, and it is carried into C++. Is it considered Best Practice to always use the latest features and innovations in a language, or stick to a more widely adopted syntax? Folks used to PooPoo TurboPascal extensions because they weren't 'Standard Pascal'. So finally my question. Can I compile a gcc for loop in cv that says for(char n=0; n < 10; n++){etc}? My favorite Imagecraft compiler cant do this yet.

Imagecraft compiler user

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

bobgardner wrote:
Evidently as time marches on, I fall further behind the Best Practices. I notice that everyone likes to declare variables at the point of use, like in for loops. This was introduced in C99 I think, and it is carried into C++. Is it considered Best Practice to always use the latest features and innovations in a language, or stick to a more widely adopted syntax? ...
Use the greatest whether or not it is the latest.
What is the greatest depends on one's criteria.
Presumably one criterion is that must compile on some set of platforms.
If that set includes a C89-only platform, code accordingly.
Otherwise I would suggest that declaring at point of use is the best for maintenance and might be best for efficiency as well.
Quote:
... Folks used to PooPoo TurboPascal extensions because they weren't 'Standard Pascal'. So finally my question. Can I compile a gcc for loop in cv that says for(char n=0; n < 10; n++){etc}? My favorite Imagecraft compiler cant do this yet.
Except for the range '0'..'9', I would not do arithmetic with char.
I'd use signed char, unsigned char, int8_t or uint8_t, but not plain char.

BTW when C89 was what was available, I did things like this:

{
unsigned char n;
for(n=0; n< 10; ++n) {
    ...
} /* n */
} /* unsigned char n */

Iluvatar is the better part of Valar.

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

Answer was non responsive to question asked. Where is that vote down button?

Imagecraft compiler user

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

Bob,

CodeVision only does C89.
You will have to ask Pavel whether he has any plans to support C99.

I tend to agree with skeeve. Code for the most general case. This will mean you can port your masterpiece to any MCU, compiler or platform.

Yes, most app note examples seem to use C99 random variable definitions. It is a pain to manually "C89" them. You can attempt to automate the process by adding an extra block with a pair of braces. However, this looks a bit messy.

David.

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

Here's another answer to vote down. I worked in digital TV receiver systems for a while. The broadcaster had five companies making hardware for them and amongst us, over the years, we had delivered about 50 different designs. In total there was a deployed population of 10 million units. Some homes had our very latest design with masses of RAM and the fastest, latest processors while some homes still had the very oldest model. We were constantly trying to persuade the broadcaster of new features or performance improvements they should deploy but were perennially being told "not possible, that 1993 model V3 only has 256KB of RAM and it couldn't cope".

Moral of story: keeping backward compatibility is all very well but you tie yourself to the lowest common denominator.

(oh and don't talk to me about MSVC and it's lack of C99. Apparently Microsoft claim that there are more important features to implement and not enough customer demand for C99 for them to bother implementing it - this is 2013 for goodness sake!)

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

I also like/use C99's initializers for structs - named struct members rather than precise ordering of constants.

C99 support is a selection criteria. I use GCC and IAR, the latter for professional work.

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

"Latest and Greatest" does not necessarily mean "Best".

In my book, Best is what works. If, for you, "works" means a variety of compilers, then you need to code for maximum portability. But, if "works" means the lowest coding error rate or the easiest maintainability, then you choice might be different.

So, it depends on you and your needs and your wants.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

I have to admit that since I've started doing it I like the idea of being able to locally define the iteration variable within for() and I just love named initalisers in structs.

Horror of horrors I've even started defining variables local to the point of usage and not immediately following an opening brace.

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

ka7ehk wrote:
"Latest and Greatest" does not necessarily mean "Best".
"Greatest" does, hence my "Use the greatest whether or not it is the latest."
The real issue is the criteria one uses, hence
ka7ehk wrote:
In my book, Best is what works. If, for you, "works" means a variety of compilers, then you need to code for maximum portability. But, if "works" means the lowest coding error rate or the easiest maintainability, then you choice might be different.
As for the original example, the answer is declare near use if the requirements allow it.
'Tis best by pretty much any criterion.
If the requirements include C89, they do not allow declare near use, so declare at the beginnings of blocks.
If the requirements include an older C compiler,
one might reconsider the requirements or their source.

Iluvatar is the better part of Valar.