Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > "Homemade" C99 prototype?

Reply
Thread Tools

"Homemade" C99 prototype?

 
 
Robert Latest
Guest
Posts: n/a
 
      12-10-2007
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-10-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-10-2007
Eric Sosman wrote:
> [...]
> #if __STDC_VERSION < 199901L


__STCD_VERSION__, of course.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Spoon
Guest
Posts: n/a
 
      12-10-2007
Eric Sosman wrote:
> Eric Sosman wrote:
>> [...]
>> #if __STDC_VERSION < 199901L

>
> __STCD_VERSION__, of course.


You typed "STCD"

<big_grin>
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      12-10-2007
"Robert Latest" <(E-Mail Removed)> 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-10-2007
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

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-11-2007
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
(E-Mail Removed)lid
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      12-24-2007
On Mon, 10 Dec 2007 08:58:07 -0500, Eric Sosman
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between "library parts" of C99 and "language parts" of C99 albert.neu@gmail.com C Programming 3 03-31-2007 08:14 PM
C99 struct initialization (C99/gcc) jilerner@yahoo.com C Programming 3 02-20-2006 04:41 AM
ISO C89 and ISO C99 jrefactors@hotmail.com C++ 13 12-20-2004 06:29 AM
How to check whether my GCC compiler support C99 standard or not? Peng Yu C++ 2 09-29-2004 04:09 PM
C99 Complex number support in C++ kartik C++ 3 07-31-2004 09:11 PM



Advertisments