From: hamish@sybase.com (Just Another Deckchair on the Titanic)
Newsgroups: comp.lang.c
Subject: Re: Convert float to a string ?
Date: 25 Aug 92 21:16:24 GMT
Organization: Sybase Inc, Emeryville CA USA

In article <1992Aug23.021013.2772@pdnfido.fidonet.org> erik@pdnfido.fidonet.org (Erik VanRiper) writes:

>>| >: 	   sprintf(b,"%f\0",a);
>>| >: 		     ^^^^^^
>>| >: 	Are you sure you need a '\0' here............???

>>| >Let me know if sprintf puts one there anyway, I wasn't sure.

>>| 	Yes. It does.

>>No, it does NOT put one there - the '\0' is implicitly provided by the
>>C compiler whenever you mention a "..." double-quoted string.  sprintf
>>does check for the NUL, however, when reading the format string.  Once
>>it reaches the NUL and copies it to the target string (in this case,
>>b), it stops.
>>
>>If you were to somehow pass a non-NUL terminated string to sprintf, it
>>would likely go off into never-never land, copying arguments and
>>trying to find the zero character.  But '\0' is guaranteed to
>>terminate any string literal compiled into a C program, therefore the
>>strings are valid input to library functions such as sprintf.
>
>Just to make an already dull conversation duller <grin>...  In the example
>above, (sprintf(b,"%f\0",a);), it does not matter if the \0 is after the %f,
>in the case of your argument above.  You could still manage to pass it a non-
>null terminated string, and it will happily barf all over the place.
 
The "\0" *is* a null terminator in the string "%f\0". Of course, it's
useless (as it's immediately followed by the null terminator put there
by the compiler as part of the correct implementation of quoted
strings...), and it doesn't in any way help ensure that a null
terminator is appended to the resulting character array pointed to by
'b', above (that's the advertised job of sprintf() - it'll do that for
you), but to scanf() the "\0" signals the end of the string. Any string
with "\0" in it *is* a null-terminated string....

>If I
>remember correctly, things are evaluated left to right, so the string will 
>attempt to be placed into b, then terminated with the \0.

I'm not sure what's being said here, but the order of evaluation of scanf's
arguments are in general undefined, and the order of evaluation of the given
example's format string (which is well-defined) will not make any difference in
this example.

>If the string
>being passed (a) is not null-terminated, the the \0 within the quotes becomes
>a waste of space, so to speak.  :-)

Again, this paragraph is a little unclear, but if by "(a)" you mean
argument 'a' in the example, 'a' is of course not meant to be a string
but a float or double; if, on the other hand, you are talking about the
format string "%f\0", or any other (quoted or not) format string with
an embedded '\0' character, then that string is inherently (and by
definition) null-terminated.

>I think we are arguing the same side here, but I figured I would stick in my
>2 cents to clear things up in my own demented way.  

Hmmm, this discussion seems to have generated some of the more, umm,
*interesting* answers about strings, null characters, scanf(), etc....


	Hamish
---------------------------------------------------------------------------
Hamish Reid       Sybase Inc, 6475 Christie Ave, Emeryville CA 94608 USA
+1 510 596-3917   hamish@netcom.com  hamish@sybase.com  uunet!sybase!hamish
Newsgroups: comp.lang.c