문제

It's 2012. I'm writing some code in C. Should I be still be using C89? Are there still compilers that do not support C99?

I don't mind using /* */ instead of //.

I'm not sure about C89 forbids mixing declarations and code. I'm kind of leaning towards the idea that it's actually more readable to have all the declarations in one place, and if it isn't, the function is too long.

VLAs look useful but I haven't needed them yet.

Should I stick with C89 if I don't have a compelling reason not to? Are there other things I haven't considered?

도움이 되었습니까?

해결책

Unless you know that you cannot use a C99-compatible compiler (the Visual Studio C compiler is the most prominent candidate) there is no good reason for not using the nice things C99 gives you.

However, even if you need to support that compiler you can use some C99 features - just not all of them.

One feature of C99 that is incredibly handy is being able to do for(int i = ...) instead of having to declare your loop variable on top of the function - especially since C actually has a block scope. That's the kind of declaration where having it on top really doesn't improve the readability.

다른 팁

There is a reason (or many) why C89 was superseded by C99. If you know for sure that no C99 compiler is available for your particular piece of work (unlikely unless you are stuck with Visual Studio which never supported C officially anyway), then you need to stay with C89 but otherwise you should certainly put yourself in a position where you can benefit from the last 20+ years of improvement. There is nothing inherently slower about C99.

Perhaps you should even consider looking at the newest C11 standard. There has been some important fixes for dealing with Unicode that any C programmer could benefit from (other changes in the standard are absolutely minimal)...

Good code is a mixture of performance, scalability, readability, and maintainability.

In my opinion, C99 makes code easier to read and maintain. Very, very few compilers don't support C99, so I say go with it. Use the tools you have available, unless you are certain you will need to compile your project with a compiler that requires the earlier standard.

Check out these links to learn more about the advantages to C99:

http://www.kuro5hin.org/?op=displaystory;sid=2001/2/23/194544/139

http://en.wikipedia.org/wiki/C99#Design

Note that C99 also supports library functions such as snprintf, which are very useful, and have better floating point support. Also, I find macros to be extremely helpful, especially when working with math-intensive applications (like cryptographic algorithms)

I disagree with Paul R's "bottom line" comment. There are multiple cases where C89 is advantageous for portability.

  1. Targeting embedded systems, which may or may not have compilers supporting C99: https://groups.google.com/forum/#!topic/comp.arch.embedded/WNvhw3T_9pI%5B1-25%5D

  2. Targeting the TinyCC compiler, as might be required in a restricted environment where installing a gigantic toolchain is either impractical or not allowed. (TCC is no longer being developed, and Bellard's last statement as to ISOC99 support was that it was "heading towards" full compliance.)

  3. Supporting dynamic compilation via libtcc (see above).

  4. Targeting MSVC, as others have noted.

  5. For source-compatibility with projects that may be required by their company to use the C89 standard. This is especially relevant if you're writing an open source library, and want to maximize its application in some industry.

As cegfault noted, some of the C99 features as listed on Wikipedia can be very useful, but none I would consider indispensable if your priority is portability, or any of the above reasons apply.

It appears Microsoft hasn't budged on C99 compliance. SimonRev from Beijer Electronics commented on a related MSDN thread in November 2016:

In broad strokes, the only parts of the C99 compiler that were implemented are those parts that they needed to keep the C++ compiler up to date.

Microsoft has done basically nothing to the C compiler since VC6, and they haven't made much secret that C++ is their vision of the future of native code, not C.

In conclusion, if you want portability for embedded or restricted systems, dynamic compilation, MSVC, or compatibility with proprietary source code, I would say C89 is advantageous.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top