4

At my university, my CS1 teacher has advised us to not use for(int i = 0; [code]; i++), because declaring a variable in line with a for loop will not compile on C versions prior to C99. As a computer science major, is there any legitimate reason for this? As it was standardized nearly two decades ago, I would think any still supported / developed software would have no trouble implementing this.

From my browsing it appears that extremely low level hardware development such as the Linux kernel may use a C90-ish codebase but that seems to be a very fringe case.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
xicronic
  • 165
  • 3
  • 1
    It depends a great deal on the target for which you are writing the code. In my opinion, I'd stick to ANSI C only on projects that I expect to to compile on embedded devices and devices with unusually-constrained development toolchains. Most of the projects I make are specific to a particular platform, and most of the platforms I interact with have modern compilers and development environments. I tend to use what is available. :-) – Jonathan Gilbert Jan 23 '17 at 20:37
  • 1
    Today the embedded field is *much* more wide-spread than before. Everyone can get a dev board and do this "extremely low level hardware development" on their kitchen. So it is not that fringe as it seems. OTOH, today most of this hardware have a modern and complying compiler to work with. So no need to stick with old standards as long as you don't have to work with some legacy tools. – Eugene Sh. Jan 23 '17 at 20:37
  • 2
    There's a compiler by a little known company — they go by the name Microsoft — that doesn't necessarily support it. It may depend on which version of MS Visual Studio you have, but their C++ support is good but their C support is more nearly archaic (especially in older versions). There are versions still out there that don't allow variables to be declared when used — they must all be defined (or declared) at the top of a statement block `{ …variable definitions…; …executable statements… }`. – Jonathan Leffler Jan 23 '17 at 20:38
  • 1
    *The sad part is that if we allow that, we also get that \*other\* insane C99 variable thing - mixing variables and code.* Written in the year 2012. Wow. – 2501 Jan 23 '17 at 20:38
  • 1
    @2501 - pretty clear he never progressed from the language that he learned – KevinDTimm Jan 23 '17 at 20:40
  • Why would you be stuck with an ancient compiler and face the wall? How would you get a job if you told the interviewer you are not allowed to use the latest C standard? "Oh yes sir, I do know them but they are terrible". We never use a microwave oven either, as pastry does not cook so well. – Weather Vane Jan 23 '17 at 20:40
  • 6
    As of 2017, there are still [compilers for embedded systems](http://tasking.com/products/c166) which don't support C99, so (sadly) it's possible you will encounter compilers which will require ANSI C. However, in my opinion, this is something people can easily adapt to, and I would prefer my students writing *simpler* code, if possible. I would say requiring students to move all variable declarations to the top of the function is *counter-productive*, if you have an actual C99 compiler. I also believe this question is rather broad/opinion based, so it is hardly a good fit for SO. – vgru Jan 23 '17 at 20:41
  • 1
    It depends on what kind of programmer you want to be when you graduate. If you get lucky and only take jobs where you will never see old C, this info is as good to you as COBOL info is to non-COBOL programmers. But if you are going to take jobs where you are going to maintain legacy code that gets dumped on you and some of that code is written in older C... it doesn't hurt to know and remember old code limitations. – abraxascarab Jan 23 '17 at 20:47
  • 1
    Now Linux kernel itself is a pretty big pre-c99 code base – user3528438 Jan 23 '17 at 20:48
  • 1
    There are codebases that require C89 in order to maintain maximum portability. But for your case, what matters is that your instructor expects it. That's reason enough. – a3f Jan 23 '17 at 20:51
  • 1
    All versions of Microsoft compilers, including Visual Studio 2015, use the old standards for any code compiled as "C", but use a mix of recent standards for code compiled as "C++". – rcgldr Jan 23 '17 at 22:29

0 Answers0