3

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: 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.

Community
  • 1
  • 1
dvanaria
  • 6,593
  • 22
  • 62
  • 82
  • 3
    The first question is a duplicate of [Where do I find the current C or C++ standard documents?](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) The answer to the second question is "that's what a compiler is for" :-) If you want a good, very-standards-conforming C99 compiler, you can use [Comeau's Online Compiler](http://www.comeaucomputing.com/tryitout/) or buy their compiler. – James McNellis Sep 29 '10 at 01:26
  • 2
    Also note that C99 is not widely supported; few compilers support it completely, many support pieces of it, and some don't support it at all (MSVC supports only C89, for example). – James McNellis Sep 29 '10 at 01:30
  • Yes, that link to the duplicate is entirely adequate, thanks (James McNellis comment). I usually look carefully at the similar questions suggested when you enter a title for a question, but I missed that one. Hopefully the second part of my question will keep this post open. I never really thought of the compiler itself as a definitive check for spec adherence, but then again, I'm just learning. That's a great insight, thanks again. – dvanaria Sep 29 '10 at 01:34
  • 2
    You're absolutely right that "if it compiles that means it conforms to the spec" is wrong. Comeau is (IMO) an exception; it uses the EDG frontend, which is one of the best there is when it comes to spec-conforming. – James McNellis Sep 29 '10 at 01:36
  • Ok, that's pretty interesting though. It seems that even Comeau is using a separate spec-conforming program right? If so, couldn't I use that 3rd party program to analyze c source code written for any compiler? Also, from what I can see from the EDG homepage (http://www.edg.com/), this isn't free software. I'd like to find a tool that is freely available. – dvanaria Sep 29 '10 at 01:47
  • @dvanaria: Recent GCC versions have pretty good support for C99 (use the `--std=c99` option). This page: http://gcc.gnu.org/c99status.html, documents where there are still holes in the support, and if you want to use an older version of GCC, there are links to corresponding pages for the older versions. – Michael Burr Sep 29 '10 at 02:18
  • Any special reason for GCC 3.4.4? That was released in 2005. If your code won't compile with more recent versions, that is likely an indication that it's not compatible with the standard, I think. I'm not being condescending, just trying to learn why you'd use an old version on purpose. – Kevin Vermeer Sep 29 '10 at 02:34
  • (reemrevnivek) Yes, I'm running gcc through a Cygwin installation on a Windows XP (SP3) installation. – dvanaria Sep 29 '10 at 02:49

4 Answers4

3

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.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
3

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 -pedantic -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. -pedantic 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.

Mihir Luthra
  • 6,059
  • 3
  • 14
  • 39
Patrick
  • 178
  • 1
  • 6
1

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
  • 233,326
  • 40
  • 323
  • 462
  • 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 Sep 29 '10 at 03:10
  • 1
    @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 Sep 29 '10 at 03:25
0

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

JoshD
  • 12,490
  • 3
  • 42
  • 53