views:

120

answers:

4

I'm continuing to learn C and would like to adhere to whatever is the current standard, but finding a good reference to that seems to be problem.

From what I've found online (mostly through Google and Wikipedia) is that the current standard used today is C99, more formally the ISO/IEC 9899:1999 standard.

When I'm writing C code, I often bring up a browser and do simple web searches for things like finding out the exact return values to the stdio.h function scanf. Mostly I just want to get into a good practice of adhering to the current standard, but even if I search for the specific string "C99 printf" or something like it, there doesn't seem to be one single place to find a definitive spec.

So I have two questions:

1) Is there a central C99 spec that is available online, maintained by the organization responsible for this standard?

[edit]: This first question has already been answered here: http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents. Thanks to James McNellis for pointing this out.

2) Is there a program that can parse a C source file to make sure it adheres to the C99 spec? I know there are programs like this to parse XHTML files and it seems like there should be one for C99 as well...

[edit]: I should also mention that I'm doing C development using gcc, specifically version 3.4.4. When I go to the main gcc website (http://gcc.gnu.org/) I'm still running into difficulty figuring out which compiler version supports which C specification.

A: 

The draft for can be obtained here for free. You can also purchase the official one from the ansi store.

JoshD
+1  A: 

The current standard for Programming Language C is ISO/IEC 9899:1999, published 1999-12-01

and

Published ISO and IEC standards can be purchased from a member body of ISO or IEC.

From your bestest buds in the standards world, ISO.

It's also worth noting the draft of the NEXT C standard is available in PDF.

TomMD
+1  A: 

Depending on what compiler you use you can ask it to check for you. For example with gcc you would run it like this: gcc -Wall -Wextra -pendantic -std=c99 -o program program.c

You would replace program.c and program with the name of your program of course.

-Wall and -Wextra provide more warnings and tell you if you have done something funky. -pendantic provides more of that as well.

you can use -ansi if you really want to follow the ansi spec personally I don't use it since I'm lazy but it is more proper.

+1  A: 

It is not actually possible to analyse a source file and conclusively determine that it complies with the C99 standard. C is different to XHTML in this regard, because C is a Turing-complete language and XHTML is not.

It is certainly possible to find many instances of non-conformance, but it's impossible to find them all. Consider, for example, a program that generates a printf format string on-the-fly - how can you statically determine that the format string will be conforming? What if it depends on the input given to the program? Consider also a program that right shifts signed integers - if the signed integer in question is never negative, then the program may be conforming, but if not then it is probably relying on implementation-defined results.

caf
I think that "may not be conforming at run time" is probably equivalent to "not conforming at all". That is, it's the opposite of "will always certainly conform".
detly
@detly: Even if we take that approach (which would have the slightly odd implication that no program using `gets()` could be conforming), it's still insoluble.
caf
@caf - I see what you mean.
detly