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


Groups > comp.lang.c > #64765

Re: sprintf and the trailing null

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: sprintf and the trailing null
Date 2015-07-06 16:38 +0100
Organization A noiseless patient Spider
Message-ID <87a8v9uwi8.fsf@bsb.me.uk> (permalink)
References <be51c03d-a8d6-4c66-b490-85466818284d@googlegroups.com> <mndvt7$299$1@dont-email.me> <4218c1d7-073c-48a4-8485-687a794848d1@googlegroups.com> <mne1fl$dm7$1@dont-email.me> <8ee8235d-f52a-4e2c-94b5-11d7b86cbc60@googlegroups.com>

Show all headers | View raw


"Rick C. Hodgin" <rick.c.hodgin@gmail.com> writes:

> On Monday, July 6, 2015 at 9:58:43 AM UTC-4, Reinhardt Behm wrote:
>> Rick C. Hodgin wrote:
>> 
>> > On Monday, July 6, 2015 at 9:31:49 AM UTC-4, John McCue wrote:
>> >> Rick C. Hodgin <rick.c.hodgin@gmail.com> wrote:
>> >> > Two questions:
>> >> >  (1) Is there a version of sprintf() without a trailing NULL?
>> >> I have not seen any, but what are you trying to accomplish ?
>> > 
>> > One example:
>> > 
>> >     char buffer[8];
>> >     sprintf(buffer, "%04u%02%02u", year, month, day);
>> > 
>> >> >  (2) Why does sprintf() automatically insert a trailing NULL?
>> >> 
>> >> You can see why if you replace the 'trailing NULL' with another
>> >> character and print out the string.
>> > 
>> > Why not options:
>> > 
>> >     sprintf0(buffer, "%04u", value);    // NULL-terminated form
>> >     sprintf(buffer, "%04u\0", value);   // Manually terminated (if needed)
>> > 
>> > Or at this point in support of legacy systems, a new function:
>> >     rawprintf(buffer, "%04u\0", value); // No NULL-termination by default
>> 
>> How does this function distinguish between these two format strings?
>> 
>> char *fmt1[] = { '%', '0', '4', 'u', '\0' } 
>> which is the same as writing "%04u"
>> and
>> char *fmt2[] = { '%', '0', '4', 'u', '\0', '\0' }
>> which is the same as writing "%04u\0"
>
> It's not the same.

You are right it isn't but not for the reason you state.  Reinhardt has
a typo -- there's an unintended * in the type.

What you wrote ("%04u\0") denotes (char)[]{'%', '0', '4', 'u', 0, 0}.
There's not way for sprintf to tell the difference between the null you
put there explicitly and the one that ends a string literal.

> The '\0' is interpreted by the compiler as a NULL.

'\0' is the same as 0 and denotes character called null.

> In order to have the sam, you'd need:
>
>     char *fmt1[] = { '%', '0', '4', 'u', '\', '0', '\0' } 
>
> ...physically embedding the \ and 0 as separate characters, then
> the trailing null.

That's not even valid C (even when the pointer is removed from the
declaration).  You might have meant '\\' in the initialiser, but that's
just as bad as adding a null character.

sprintf already has the right mechanism -- the % syntax.  You could
propose a new conversion specifier which, like %%, denotes a single
character -- a null.  It would mainly be useful in your memprintf
function:

  memprintf(buffer, "%4u%!", year); // terminated -- min 5 bytes written
  memprintf(buffer, "%4u%!", year); // min 4 bytes written, no null

>> For the rare case you can always write your above example as
>>      char buffer[8];
>>      char buffer1[sizeof(buffer) + 1];
>>      sprintf(buffer1, "%04u%02%02u", year, month, day);
>>      strncpy(buffer, buffer1, sizeof(buffer));
>
> Yes.  I'm thinking this double-processing could be avoided with
> rawprintf().

You can avoid the double copy (in many cases) like this:

     union {
          char unterminated[8];
          char string[8 + 1];
     } buffer;
     sprintf(buffer.string, "%04u%02u%02u", year, month, day);

and then use buffer.unterminated with its helpfully worrying name.
Alternatively, you could write:

     char buffer[8];
     snprintf(buffer, sizeof buffer, "%04u%02u%02u", year, month, day);
     buffer[sizeof buffer - 1] = '0' + day % 10;

which is not very nice, but it's safer all round because you can't
overflow the buffer due to bad values in the printed variables.

<snip>
-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 06:16 -0700
  Re: sprintf and the trailing null John McCue <jmccue@jmcnet1.bstnma.east.verizon.net> - 2015-07-06 13:30 +0000
    Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 06:37 -0700
      Re: sprintf and the trailing null Reinhardt Behm <rbehm@hushmail.com> - 2015-07-06 21:58 +0800
        Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 07:12 -0700
          Re: sprintf and the trailing null Reinhardt Behm <rbehm@hushmail.com> - 2015-07-06 22:34 +0800
            Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 08:44 -0700
          Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-06 15:46 +0100
            Re: sprintf and the trailing null Keith Thompson <kst-u@mib.org> - 2015-07-06 08:06 -0700
              Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-06 17:42 +0100
                Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 09:48 -0700
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-06 18:07 +0100
                Re: sprintf and the trailing null Martin Shobe <martin.shobe@yahoo.com> - 2015-07-06 12:40 -0500
                Re: sprintf and the trailing null Keith Thompson <kst-u@mib.org> - 2015-07-06 10:46 -0700
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-06 18:58 +0100
          Re: sprintf and the trailing null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-06 16:38 +0100
            Re: sprintf and the trailing null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-06 23:44 +0100
              Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 15:48 -0700
              Re: sprintf and the trailing null Phil Carmody <pc+usenet@asdf.org> - 2015-07-08 20:09 +0300
                Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-08 10:11 -0700
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-08 19:01 +0100
                Re: sprintf and the trailing null David Kleinecke <dkleinecke@gmail.com> - 2015-07-09 09:02 -0700
                Re: sprintf and the trailing null Richard Heathfield <rjh@cpax.org.uk> - 2015-07-09 17:07 +0100
                Re: sprintf and the trailing null Phil Carmody <pc+usenet@asdf.org> - 2015-07-10 17:06 +0300
                Re: sprintf and the trailing null glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-07-09 17:42 +0000
                Re: sprintf and the trailing null Phil Carmody <pc+usenet@asdf.org> - 2015-07-10 13:33 +0300
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-10 12:09 +0100
                Re: sprintf and the trailing null Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-10 04:51 -0700
                Re: sprintf and the trailing null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-10 17:24 +0100
                Re: sprintf and the trailing null Keith Thompson <kst-u@mib.org> - 2015-07-10 08:33 -0700
                Re: sprintf and the trailing null Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 04:58 -0700
      Re: sprintf and the trailing null jt@toerring.de (Jens Thoms Toerring) - 2015-07-06 14:15 +0000
        Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 07:20 -0700
          Re: sprintf and the trailing null raltbos@xs4all.nl (Richard Bos) - 2015-07-06 19:54 +0000
            Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 13:31 -0700
              Re: sprintf and the trailing null Richard Heathfield <rjh@cpax.org.uk> - 2015-07-06 21:52 +0100
              Re: sprintf and the trailing null raltbos@xs4all.nl (Richard Bos) - 2015-07-06 22:52 +0000
                Re: sprintf and the trailing null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 01:04 +0100
                Re: sprintf and the trailing null Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-06 19:13 -0700
                Re: sprintf and the trailing null Reinhardt Behm <rbehm@hushmail.com> - 2015-07-07 12:45 +0800
                Re: sprintf and the trailing null Rosario19 <Ros@invalid.invalid> - 2015-07-07 10:15 +0200
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-07 10:31 +0100
                Re: sprintf and the trailing null Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-07 02:51 -0700
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-07 12:10 +0100
                Re: sprintf and the trailing null Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-07 05:08 -0700
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-07 13:52 +0100
                Re: sprintf and the trailing null Phil Carmody <pc+usenet@asdf.org> - 2015-07-08 20:20 +0300
                Re: sprintf and the trailing null Bartc <bc@freeuk.com> - 2015-07-08 18:51 +0100
                Re: sprintf and the trailing null raltbos@xs4all.nl (Richard Bos) - 2015-07-09 10:27 +0000
                Re: sprintf and the trailing null Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-10 19:03 -0700
              Re: sprintf and the trailing null gordonb.7tw58@burditt.org (Gordon Burditt) - 2015-07-07 15:40 -0500
                Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-07 15:14 -0700
        Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 07:27 -0700
          Re: sprintf and the trailing null jt@toerring.de (Jens Thoms Toerring) - 2015-07-06 16:46 +0000
        Re: sprintf and the trailing null Keith Thompson <kst-u@mib.org> - 2015-07-06 07:56 -0700
  Re: sprintf and the trailing null Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-07-06 09:32 -0600
  Re: sprintf and the trailing null Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-07-06 08:37 -0700
    Re: sprintf and the trailing null Keith Thompson <kst-u@mib.org> - 2015-07-06 08:54 -0700
  Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 08:53 -0700
    Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 09:00 -0700
    Re: sprintf and the trailing null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-06 17:14 +0100
      Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 09:24 -0700
        Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 09:31 -0700
  Re: sprintf and the trailing null Sjouke Burry <burrynulnulfour@ppllaanneett.nnll> - 2015-07-07 01:43 +0200
    Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 17:09 -0700
      Re: sprintf and the trailing null Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2015-07-06 21:08 -0600
        Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-06 21:29 -0700
        Re: sprintf and the trailing null Phil Carmody <pc+usenet@asdf.org> - 2015-07-08 20:24 +0300
          Re: sprintf and the trailing null "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2015-07-08 10:44 -0700
  Re: sprintf and the trailing null Les Cargill <lcargill99@comcast.com> - 2015-07-07 21:48 -0500

csiph-web