stringizing

Jim Patterson jimp at cognos.UUCP
Thu Apr 25 03:52:46 AEST 1991


In article <1991Apr23.075527.19443 at tkou02.enet.dec.com> diamond at jit345.enet@tkou02.enet.dec.com (Norman Diamond) writes:
>In article <14888 at darkstar.ucsc.edu> daniel at terra.ucsc.edu (Daniel Edelson) writes:
>>	#define  str(x) #x
>>		printf("%s\n", str(  hello\t\n\vworld\n\n));
>>Is the output from this program supposed to be:
>>	hello world
>
>Section 2.1.1.2, page 6 lines 35-36 in the Dec. 1988 draft:
>  Whether each nonempty sequence of white-space characters other than newline
>  is retained or replaced by one space character is implementation-defined.
>[This is before preprocessing.]
>
>Therefore, the output can be either (with two leading spaces):
>  hello\t\n\vworld\n\n\n
>or (with one leading space and two imbedded spaces):
> hello \n world\n\n\n
>(including, in each case, the last \n from the printf format string).

You seem to be assuming that \t \v etc are whitespace characters. They
aren't; they are escape sequences. 

The point at which escape characters are converted to characters is
clearly defined by the standard section 2.1.1.2 Translation Phases.
During step 4, "preprocessing directives are executed and macros
expanded". 

At this point, the str(...) macro should be expanded. \t and \v should
still be escape sequences at this point because in step 5 it's
specified that "escape sequences in character constants and string
literals are converted to members of the execution character set".

Note that \t \v etc are NOT translated except in character and string
constants. Their appearance in other contexts is not valid (though I
think the above example is valid).

My interpretation is that the entire string within the str() with
the exception of the leading white space is string-ized. That is,
the result of str(   hello\t\n\vworld\n\n) is "hello\t\n\vworld\n\n".

(Section 3.8.3.2 explains the string-ize function and it clearly
states that leading and trailing whitespace is deleted while internal
whitespace sequences are replaced by single spaces).
-- 
Jim Patterson                              Cognos Incorporated
UUCP:uunet!mitel!cunews!cognos!jimp        P.O. BOX 9707    
PHONE:(613)738-1440 x6112                  3755 Riverside Drive
                                           Ottawa, Ont  K1G 3Z4



More information about the Comp.std.c mailing list