Thread: The difference between C89 and C99

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    The difference between C89 and C99

    I have a project that I have written in C99. It uses variadic macros, new-style function definitions, and variable declarations can occur below statements. These things are outside the scope of the original C89 draft, so they're technically C99. However, a guy working with me has stated that he'd like to transition it to C89 for greater compiler compatibility.

    I sometimes get confused about exactly what is considered C89 and what is not. New-style function definitions are ubiquitous, but do people consider that they're writing in C99 if they use them?

    I'd like my project to be as portable as possible. Realistically, how many compilers that are worth using are going to fail to compile my code?

    Richard

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What is new-style function definitions??
    AFIAK, Microsoft VC does not support C99.
    But gcc,intel Ccompiler, Pelles C all support C99.(at least most C99 features).

    vardiadic macro is just C pre processor. You could just use C99 cpp.
    variable declaration anywhere is most likely supported in C/C++ compiler since C++ also allows.
    Unless you're using restrict,VLA,compound literal,complex numbers,...... your code should be quite portable...
    Last edited by Bayint Naung; 03-22-2011 at 05:46 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    C99 stuff

    New style function definitions means:
    Code:
    int myfunction (int x, int y)
    {
    }
    instead of:
    Code:
    int myfunction (x,y)
    int x, y;
    {
    }
    I use compound string literals, like this: "Error occurred at line " __LINE__ , since it's so useful. But when you say MSVC doesn't support C99, it supports most of it.

    Richard

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I sometimes get confused about exactly what is considered C89 and what is not. New-style function definitions are ubiquitous, but do people consider that they're writing in C99 if they use them?
    No because that's in C89 too.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >>I use compound string literals, like this: "Error occurred at line " __LINE__ , since it's so useful. But when you say MSVC doesn't support C99, it supports most of it.
    Already in C89.
    Your new function definition is not new. The former is K&R. Later C89...
    C99 does not allow explicit int.
    like in
    Code:
     foo(void) {  //error no explicit int
      return 3;
     }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Index of /JTC1/sc22/wg14/www/docs/n869
    Quote Originally Posted by ISO committee
    — restricted character set support in <iso646.h> (originally specifed in AMD1)
    — wide-character library support in <wchar.h> and <wctype.h> (originally specifed in AMD1)
    — restricted pointers
    — variable-length arrays
    — fexible array members
    — complex (and imaginary) support in <complex.h>
    — type-generic math macros in <tgmath.h>
    — the long long int type and library functions
    — increased translation limits
    — remove implicit int
    — the vscanf family of functions
    — reliable integer division
    — universal character names
    — extended identifers
    — binary foating-point literals and printf/scanf conversion specifers
    — compound literals
    — designated initializers
    — // comments
    — extended integer types in <inttypes.h> and <stdint.h>
    — remove implicit function declaration
    — preprocessor arithmetic done in intmax_t/uintmax_t
    — mixed declarations and code
    — integer constant type rules
    — integer promotion rules
    — vararg macros
    — additional math library functions in <math.h>
    — foating-point environment access in <fenv.h>
    — IEC 60559 (also known as IEC 559 or IEEE arithmetic) support
    — trailing comma allowed in enum declaration
    — %lf conversion specifer allowed in printf
    — inline functions
    — the snprintf family of functions
    — boolean type in <stdbool.h>
    — idempotent type qualifers
    — empty macro arguments
    — new struct type compatibility rules (tag compatibility)
    — _Prama preprocessing operator
    — standard pragmas
    — __func__ predefned identifer
    — VA_COPY macro
    — additional strftime conversion specifers
    — LIA compatibility annex
    — deprecate ungetc at the beginning of a binary file
    — remove deprecation of aliased array parameters
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    C99 it is

    C99 is the only one that supports:

    - compound literals
    - variadic macros
    - mixing declarations and statements
    - snprintf
    - bools in <stdbool.h>

    all of which my code uses. So I'm using C99. But as I understand it, most people don't refer to these concepts as C99.

    Realistically, is there any compiler worth using that won't allow you to mix declarations and statements?

    Richard
    Last edited by Richardcavell; 03-22-2011 at 07:05 PM. Reason: booleans, too

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    But as I understand it, most people don't refer to these concepts as C99.
    Who is "most people" and aren't they wrong regardless?

    Realistically, is there any compiler worth using that won't allow you to mix declarations and statements?
    All of them, if you aren't using the --std=c99 switch, or something like it.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I hate mixed declarations. They are for lazy people who don't think ahead.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Mixed declarations

    I like mixed declarations in the following scenarios:

    Code:
    struct mystruct s = { 1 , 2 , 3 };
    especially when I've declared one or more parts of the structure as const. And what about this? How do you do this in C89 without wasting memory?

    Code:
    int length = strlen (mystring);
    char szcopy [length+1];
    strcpy (szcopy, mystring);
    Last edited by Richardcavell; 03-22-2011 at 08:53 PM. Reason: oops

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Since C89 does not have Variable Length Arrays you would need to use malloc/free to dynamically allocate the memory.

    Jim

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    What about struct initialization?

    What about struct initialization? You could convert:

    Code:
    struct mystruct s = { 1, 2, 3 };
    into :
    Code:
    struct mystruct;
    mystruct.one = 1;
    mystruct.two = 2;
    mystruct.three = 3;
    but then you can't declare any struct members const (which I have done as an error-checking measure).

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    struct mystruct s = { 1 , 2 , 3 };
    This is not mixed declarations and code. This is an initializer list, which is perfectly valid C89.

    There is no standard C89 way of doing VLAs, and indeed, without VLAs I'm not sure C99 would have supported mixed declarations and code. Perhaps yes, because it's a common extension to C89, but like in C++, other language features make it a practical necessity.

    alloca() is a common (at least in the Unix world) way of obtaining VLA-like behavior in C89, but it is not standard—not even POSIX.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    Quote Originally Posted by quzah View Post
    I hate mixed declarations. They are for lazy people who don't think ahead.


    Quzah.
    Quzah, do you think it's okay to use initializers in a declaration? I mean, it amounts to code if it's anything other than a constant.

    Richard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C89 or C99
    By talin in forum C Programming
    Replies: 6
    Last Post: 05-26-2008, 12:45 PM
  2. C99 C89
    By salvadoravi in forum C Programming
    Replies: 4
    Last Post: 01-21-2008, 07:43 AM
  3. My C89 RANT!
    By evildave in forum C Programming
    Replies: 12
    Last Post: 12-07-2005, 10:15 PM
  4. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  5. C89 and C99
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 02-24-2005, 12:21 AM

Tags for this Thread