Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

snprintf macro test?
What is a good way to test for snprintf? I tried:

#if __STDC_VERSION__ >= 199901L
buf += snprintf(buf, blim - buf,
#else
buf += sprintf(buf,
#endif

but gcc doesn't set pick this up even with -D_ISOC99_SOURCE.

What's the best way?

Thanks,
Mike

Report this thread to moderator Post Follow-up to this message
Old Post
Michael B Allen
09-22-04 08:58 AM


Re: snprintf macro test?
On Wed, 22 Sep 2004 01:17:22 -0400, Michael B Allen wrote:

> What is a good way to test for snprintf? I tried:
>
> #if __STDC_VERSION__ >= 199901L
>     buf += snprintf(buf, blim - buf,

This doesn't work...

char real_buf[4];
char *buf = real_buf;
char *blim = &real_buf[sizeof(real_buf)];

/* blim - buf == 4, ret = 2, written = 3 */
buf += snprintf(buf, blim - buf, "%s", "xy");

/* blim - buf == 2, ret = 2, written = 2 */
buf += snprintf(buf, blim - buf, "%s", "xy");

/* blim - buf == 0, ret = 2, written = 0 */
buf += snprintf(buf, blim - buf, "%s", "xy");

/* blim - buf == (size_t)-2, ret = 2, written = 3 */
buf += snprintf(buf, blim - buf, "%s", "xy");

...it also doesn't work when sprintf() returns -1.

> #else
>     buf += sprintf(buf,
> #endif
>
> but gcc doesn't set pick this up even with -D_ISOC99_SOURCE.
>
> What's the best way?

autoconf, or something very similar as you'll often need to check for
things like _snprintf() and what the return value is.
A much better question though is what do you do if it isn't there?

I'd highly recommend just having your own sprintf() on a real string type
that way it's very simple to have an appending version.
See:

http://www.and.org/texts/simple_printf.c.html
http://www.and.org/vstr/printf_comparison.html

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/


Report this thread to moderator Post Follow-up to this message
Old Post
James Antill
09-22-04 09:01 PM


Re: snprintf macro test?
On Wed, 22 Sep 2004 12:52:17 -0400, James Antill wrote: 
>
>  This doesn't work...

True. You got me!

>  I'd highly recommend just having your own sprintf() on a real string
>  type
> that way it's very simple to have an appending version.

I've seen your vstr library but I'd rathing stick to plain strings at
the moment. Is there a good stand-alone snprintf that doesn't exibit
the odd c99 semantics?

Thanks,
Mike

Report this thread to moderator Post Follow-up to this message
Old Post
Michael B Allen
09-23-04 08:56 AM


Re: snprintf macro test?
"Michael B Allen" <mba2000@ioplex.com> wrote, on Wed, 22 Sep 2004:

> #if __STDC_VERSION__ >= 199901L
[...]
> but gcc doesn't set pick this up even with -D_ISOC99_SOURCE.

$ echo __STDC_VERSION__ | gcc -E - | tail -n 1
__STDC_VERSION__
$ echo __STDC_VERSION__ | gcc -std=c99 -E - | tail -n 1
199901L

--
Geoff Clare <nospam@gclare.org.uk>

Report this thread to moderator Post Follow-up to this message
Old Post
Geoff Clare
09-23-04 08:58 PM


Re: snprintf macro test?
On Thu, 23 Sep 2004 08:44:49 -0400, Geoff Clare wrote:

> "Michael B Allen" <mba2000@ioplex.com> wrote, on Wed, 22 Sep 2004:
> 
> [...] 
>
> $ echo __STDC_VERSION__ | gcc -E - | tail -n 1 __STDC_VERSION__ $ echo
> __STDC_VERSION__ | gcc -std=c99 -E - | tail -n 1 199901L
>

Thanks.

So what does _ISOC99_SOURCE do exactly?

Mike

Report this thread to moderator Post Follow-up to this message
Old Post
Michael B Allen
09-23-04 08:58 PM


Re: snprintf macro test?
On Thu, 23 Sep 2004 02:20:52 -0400, Michael B Allen wrote:

> On Wed, 22 Sep 2004 12:52:17 -0400, James Antill wrote: 
>
> I've seen your vstr library but I'd rathing stick to plain strings at
> the moment. Is there a good stand-alone snprintf that doesn't exibit
> the odd c99 semantics?

Well the comparison page[1] isn't specific to Vstr, and there are
certainly other string libraries. But if you want one that you can just cp
into a project using C-style string then I'd probably recommend trio[2].

[1] http://www.and.org/vstr/printf_comparison.html

[2] http://www.and.org/vstr/printf_comparison.html#trio ;)

--
James Antill -- james@and.org
Need an efficient and powerful string library for C?
http://www.and.org/vstr/


Report this thread to moderator Post Follow-up to this message
Old Post
James Antill
09-23-04 08:58 PM


Re: snprintf macro test?
"Michael B Allen" <mba2000@ioplex.com> wrote, on Thu, 23 Sep 2004:
 
>
> Thanks.
>
> So what does _ISOC99_SOURCE do exactly?

It makes C99 symbols visible in the headers.  However, __STDC_VERSION__
is defined internally by the compiler, it isn't defined in a header.

There shouldn't be any need to define _ISOC99_SOURCE explicitly with -D.
It will be turned on automatically if -std=c99 is used or if a superset
of C99 is selected, e.g. -D_XOPEN_SOURCE=600 for SUSv3.

--
Geoff Clare <nospam@gclare.org.uk>

Report this thread to moderator Post Follow-up to this message
Old Post
Geoff Clare
09-24-04 09:02 PM


Re: snprintf macro test?
On Thu, 23 Sep 2004 10:42:12 -0400, James Antill wrote: 
>
>  Well the comparison page[1] isn't specific to Vstr, and there are
> certainly other string libraries. But if you want one that you can just
> cp into a project using C-style string then I'd probably recommend
> trio[2].
>
> [1] http://www.and.org/vstr/printf_comparison.html

Mmm, I didn't look at this very carefully the first time. It's a nice
list. I looked at the BSD code but unfortunately it looks intertwined
with the stdio code.

I'll look at trio.

Thanks,
Mike

Report this thread to moderator Post Follow-up to this message
Old Post
Michael B Allen
09-24-04 09:02 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:14 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.