Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.arch.embedded > #28434

Re: snprintf return value

From Hans-Bernhard Bröker <HBBroeker@t-online.de>
Newsgroups comp.arch.embedded
Subject Re: snprintf return value
Date 2018-12-11 00:47 +0100
Message-ID <g78c9gFctonU1@mid.dfncis.de> (permalink)
References <pulekl$n55$1@dont-email.me>

Show all headers | View raw


Am 10.12.2018 um 11:20 schrieb pozz:
> I usually have a static buffer and I don't want to use a dynamic buffer.
> 
> char buf[BUFLEN];
> 
> I have to construct a long string, and it is quite easy to call snprintf
> multiple times:
> 
>   int n = 0;
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   n += snprintf(&buf[n], BUFLEN - n, ...);

What exactly do you expect to gain by doing it that way?

> I was convinced the return value was the real number of characters
> written in buf, 

Convinced by what?  I mean, come on, what do we (as a community) pay
documentation writers for, if nobody can be bothered to read what they
created?

> The return value of snprintf() is useful if you want to dynamically
> reallocate the buffer so it can store all the string. 

And that's not at all a coincidence.  It's precisely the reason why the
return value of v?snprintf() is defined slightly differently from that
of all the other *printf() functions.

> In my case I would
> prefer to truncate the string at the end of the static buffer. The above
> code should be redesigned as:
> 
>   int n = 0;
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   if (n >= BUFLEN - n) return;
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   if (n >= BUFLEN - n) return;
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   if (n >= BUFLEN - n) return;
>   n += snprintf(&buf[n], BUFLEN - n, ...);
>   if (n >= BUFLEN - n) return;

Or much simpler as

   snprintf(buf, BUFLEN,
      ...
      ...
      ...
      ...
   );
   buf[BUFLEN - 1] = '\0';  // just in case we hit bottom

That said, if you absolutely have to, there's always the %n output format...

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

snprintf return value pozz <pozzugno@gmail.com> - 2018-12-10 11:20 +0100
  Re: snprintf return value Reinhardt Behm <rbehm@hushmail.com> - 2018-12-10 18:24 +0800
    Re: snprintf return value pozz <pozzugno@gmail.com> - 2018-12-10 11:32 +0100
      Re: snprintf return value Philipp Klaus Krause <pkk@spth.de> - 2019-01-01 12:59 +0100
  Re: snprintf return value David Brown <david.brown@hesbynett.no> - 2018-12-10 13:15 +0100
  Re: snprintf return value Clifford Heath <no.spam@please.net> - 2018-12-11 08:49 +1100
    Re: snprintf return value Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2018-12-11 11:41 -0500
      Re: snprintf return value Clifford Heath <no.spam@please.net> - 2018-12-12 10:34 +1100
        Re: snprintf return value David Brown <david.brown@hesbynett.no> - 2018-12-12 09:09 +0100
          Re: snprintf return value Clifford Heath <no.spam@please.net> - 2018-12-12 21:38 +1100
        Re: snprintf return value Les Cargill <lcargill99@comcast.com> - 2018-12-30 11:23 -0600
  Re: snprintf return value Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2018-12-11 00:47 +0100
    Re: snprintf return value Clifford Heath <no.spam@please.net> - 2018-12-11 13:31 +1100

csiph-web