[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::decc

Title:DECC
Notice:General DEC C discussions
Moderator:TLE::D_SMITHNTE
Created:Fri Nov 13 1992
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2212
Total number of notes:11045

2149.0. "sprintf and trailing %" by RDGENG::ddors.reo.dec.com::readings_r () Thu Apr 10 1997 08:17

sprintf() on Digital UNIX gets confused by a trailing %, which really 
shouldn't be there anyway (should be %s, %d, ..., %%, or nothing at 
all).

For example the following code:

#include <stdio.h>
#include <errno.h>

main()
{
        extern int errno;
        char * g_outBuf = (char *) malloc(1024);
        int r, count = 5;

        r = sprintf (g_outBuf, "SET^DOSET^%s^%d%", "source", count);

        printf ("count = %d, g_outBuf = <%s>\n", count, g_outBuf);
        printf ("r = %d, errno = %d\n", r, errno);
}

prints this under Digital UNIX:

        count = 5, g_outBuf = <>
        r = -1, errno = 0

but under other OS's, e.g. NT:

count = 5, g_outBuf = <SET^DOSET^source^5>
r = 18, errno = 0

This is somewhat unusual behavior, since sprintf() under other OS's 
(e.g. NT) will write stuff into the buffer up to the point of the 
first failure (which in this case is the end of the format string).

Which is the correct behaviour?

Richard
T.RTitleUserPersonal
Name
DateLines
2149.1NT is correctTAVENG::BORISBoris Gubenko, ISEThu Apr 10 1997 10:1622
  NT exhibits the correct behavior. A single "%" presents an illegal conversion
  specification. The result of processing such specification in not specified
  by the standard.

  From XPG4, for example,  "if a conversion specification does not match one
  of the above forms, the behavior is undefined".

  However, each legal conversion specification must result in fetching one or
  more arguments and converting, formatting and printing them.

  The function also should not return -1 due to illegal conversion
  specification.

  From the Return Value section:

  ... these function return ... a negative value if an output error was
  encountered.

  Since this does not relate to DEC C, I'd suggest to post it in
  TURRIS::DIGITAL_UNIX.

  Boris