An interesting behaviour in printf

John Woods john at frog.UUCP
Tue Mar 21 10:51:00 AEST 1989


In article <2343 at buengc.BU.EDU>, bph at buengc.BU.EDU (Blair P. Houghton) writes:
> In article <15938 at cup.portal.com> Tim_CDC_Roberts at cup.portal.com writes:
> >In <960 at Portia.Stanford.EDU>, joe at hanauma (Joe Dellinger) asks:
> >>	string[0] = '*';
> >>	string[1] = '\0';
> >>	printf("%s\n", string[1]);
> >Trivia question: is the '(null)' output of printf standard or widespread?
> The uVAX/Ultrix C compiler ignores it whole.  The only thing the program emits
> is the \n.  (I checked it by "funkyprint | od -bc".  One char out.  \012.
> More interesting than I expected.  Smells broken.)
> The Encore/Umax C compiler emits \022 \012.  So, like, what's \022?  My
> ASCII table says it's a "dc2".

I'd bet dollars to holes in doughnuts that Ultrix's library (note: not the
compiler) behaves that way because there are four zero-bytes at address 0.
The first of them looks like an empty string ("").  Or maybe they check for
NULL and call it an empty string.

The program running on the Encore probably sees \022\040\000 at address 0.
A string with two characters, \022 and space.

Note that there are some systems that could potentially bus-error instead
of printing anything.  To quote Henry's Ten Commandments, "2.  Thou shalt
not follow the NULL pointer, for chaos and madness await thee at its end."
(a wonderful set of dicta for printing in Fraktur and posting on one's wall
:-)

UNOS' printf routine displays a NULL %s value as <NULL POINTER>, a bit more
verbose than (null) but equally useful.

-- 
John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101
...!decvax!frog!john, john at frog.UUCP, ...!mit-eddie!jfw, jfw at eddie.mit.edu

"He should be put in stocks in Lafeyette Square across from the White House
 and pelted with dead cats."	- George F. Will



More information about the Comp.lang.c mailing list