"Homemade" C99 prototype?

Discussion in 'C Programming' started by Robert Latest, Dec 10, 2007.

  1. Hello,

    generally I stick with C89, but C99 is useful at times, such as the
    snprintf() function.

    To use that function within an otherwise C89-conforming program, I just put
    the proper prototype into my program. The linker links against the C99
    library, so everything runs through without error or warning. My question
    is: Is this good practice?

    I can't see any problems because:

    1. CC without C99 header, LD against C99 lib -> no problem
    2. CC with C99 header, LD against C99 lib -> no problem
    3. CC with with non-standard header (snprintf() defined
    in some other way) -> compiler error
    4. CC without non-standard header, LD against non-standard
    lib -> runtime error

    Only the last case is a problem, but that would be such a profoundly goofy
    implementation of some non-C language that I wouldn't have to worry about
    it.

    Am I right?

    robert
    Robert Latest, Dec 10, 2007
    #1
    1. Advertising

  2. Robert Latest

    Eric Sosman Guest

    Robert Latest wrote:
    > Hello,
    >
    > generally I stick with C89, but C99 is useful at times, such as the
    > snprintf() function.
    >
    > To use that function within an otherwise C89-conforming program, I just put
    > the proper prototype into my program. The linker links against the C99
    > library, so everything runs through without error or warning. My question
    > is: Is this good practice?
    >
    > I can't see any problems because:
    >
    > 1. CC without C99 header, LD against C99 lib -> no problem
    > 2. CC with C99 header, LD against C99 lib -> no problem
    > 3. CC with with non-standard header (snprintf() defined
    > in some other way) -> compiler error
    > 4. CC without non-standard header, LD against non-standard
    > lib -> runtime error
    >
    > Only the last case is a problem, but that would be such a profoundly goofy
    > implementation of some non-C language that I wouldn't have to worry about
    > it.


    In case 2, I think you'll need not only a C99 <stdio.h>
    but also a C99 compiler, because a C90 compiler will choke
    on the `restrict' keyword. Also, the <stdio.h> declaration
    will use `restrict' but your free-hand version presumably
    will not, so the declarations of snprintf() conflict with
    each other. (Well, they are at least "different." A brief
    scan of a few sections of the Standard leaves me baffled as
    to whether the declarations "conflict" strongly enough to
    require a diagnostic; let's leave it to the language lawyers.)

    I can't think of a really "good" way to solve the problem
    completely, but adding a guard like

    #if __STDC_VERSION < 199901L
    int snprintf(char *, size_t, const char *, ...);
    #endif

    .... may smooth over a few rough spots.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 10, 2007
    #2
    1. Advertising

  3. Robert Latest

    Eric Sosman Guest

    Eric Sosman wrote:
    > [...]
    > #if __STDC_VERSION < 199901L


    __STCD_VERSION__, of course.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 10, 2007
    #3
  4. Robert Latest

    Spoon Guest

    Eric Sosman wrote:
    > Eric Sosman wrote:
    >> [...]
    >> #if __STDC_VERSION < 199901L

    >
    > __STCD_VERSION__, of course.


    You typed "STCD"

    <big_grin>
    Spoon, Dec 10, 2007
    #4
  5. "Robert Latest" <> wrote in message
    > generally I stick with C89, but C99 is useful at times, such as the
    > snprintf() function.
    >
    > To use that function within an otherwise C89-conforming program, I just
    > put
    > the proper prototype into my program. The linker links against the C99
    > library, so everything runs through without error or warning. My question
    > is: Is this good practice?
    >

    No. It won't compile unless a C99 library is present.
    The correct way to solve the problem is to write a function that calculates
    the maximum length of a printf()-style format string. You then use that to
    ensure vsprintf() doesn't overflow its buffer.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Dec 10, 2007
    #5
  6. Robert Latest

    CBFalconer Guest

    Eric Sosman wrote:
    > Eric Sosman wrote:
    >> [...]
    >> #if __STDC_VERSION < 199901L

    >
    > __STCD_VERSION__, of course.


    Yes, those systems require stereo CDs, of course :)

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee.
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Dec 10, 2007
    #6
  7. Robert Latest

    Eric Sosman Guest

    Spoon wrote:
    > Eric Sosman wrote:
    >> Eric Sosman wrote:
    >>> [...]
    >>> #if __STDC_VERSION < 199901L

    >>
    >> __STCD_VERSION__, of course.

    >
    > You typed "STCD"
    >
    > <big_grin>


    Give me enough rope, and I'll type __STCD_VERIZON__

    (Sigh. Some days, it doesn't pay to get out of bed.)

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 11, 2007
    #7
  8. On Mon, 10 Dec 2007 08:58:07 -0500, Eric Sosman
    <> wrote:

    > Robert Latest wrote:

    <snip: hand-declare snprintf in various situations>
    > > 2. CC with C99 header, LD against C99 lib -> no problem

    <snip>
    > In case 2, I think you'll need not only a C99 <stdio.h>
    > but also a C99 compiler, because a C90 compiler will choke
    > on the `restrict' keyword. Also, the <stdio.h> declaration
    > will use `restrict' but your free-hand version presumably
    > will not, so the declarations of snprintf() conflict with
    > each other. (Well, they are at least "different." A brief
    > scan of a few sections of the Standard leaves me baffled as
    > to whether the declarations "conflict" strongly enough to
    > require a diagnostic; let's leave it to the language lawyers.)
    >

    Not. Multiple (co-visible) declarations of the same function must give
    it _compatible_ types, and for function types in 6.7.5.3p15 among
    other things: "(In the determination of type
    compatibility ... each parameter declared with qualified type
    is taken as having the unqualified version of its declared type.)"

    This is because arguments are passed by (r)value in C, and the
    original (C89) qualifiers (const, volatile) don't apply to rvalues,
    only to lvalues/objects. C99 restrict is syntactically also a
    qualifier, although semantically it sort-of bridges from the
    so-qualified lvalue to the (pointer) rvalue stored therein.

    The OP's technique is arguable -- with a C99 lib/headers it's
    redundant, and with any other lib/headers you aren't guaranteed and in
    practice won't get any checking that you matched the definition, and
    if you don't IME the presence of the apparently-correct declaration
    tends to confuse matters and impede diagnosis. But it will compile.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Dec 24, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    3,646
    Chris Torek
    Feb 20, 2006
  2. Bubba
    Replies:
    1
    Views:
    265
    Jorgen Grahn
    Apr 16, 2011
  3. Simon Wigzell

    Homemade website traffic tracking reports?

    Simon Wigzell, Sep 1, 2003, in forum: ASP General
    Replies:
    1
    Views:
    93
    Curt_C [MVP]
    Sep 1, 2003
  4. Simon Strandgaard
    Replies:
    1
    Views:
    122
    Michael Neumann
    Aug 3, 2004
  5. Randy Kramer
    Replies:
    8
    Views:
    149
    Randy Kramer
    Mar 2, 2005
Loading...

Share This Page