[comp.std.c] stringizing

daniel@terra.ucsc.edu (Daniel Edelson) (04/23/91)

Given the following program:

	#include <stdio.h>
	#define  str(x) #x
	int main(void)
	{
		printf("%s\n", str(  hello\t\n\vworld\n\n));
		return 0;
	}

Is the output from this program supposed to be:

	hello world

Thanks,
Daniel Edelson

diamond@jit345.swstokyo.dec.com (Norman Diamond) (04/23/91)

In article <14888@darkstar.ucsc.edu> daniel@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).

However, it cannot be (regardless of the number of leading spaces) just:
 hello world\n
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.

jimp@cognos.UUCP (Jim Patterson) (04/25/91)

In article <1991Apr23.075527.19443@tkou02.enet.dec.com> diamond@jit345.enet@tkou02.enet.dec.com (Norman Diamond) writes:
>In article <14888@darkstar.ucsc.edu> daniel@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

diamond@jit533.swstokyo.dec.com (Norman Diamond) (04/26/91)

In article <9565@cognos.UUCP> jimp@cognos.UUCP (Jim Patterson) writes:
>In article <1991Apr23.075527.19443@tkou02.enet.dec.com> diamond@jit345.enet@tkou02.enet.dec.com (Norman Diamond) writes:
>>In article <14888@darkstar.ucsc.edu> daniel@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
>>  Whether each nonempty sequence of white-space characters other than newline
>>  is retained or replaced by one space character is implementation-defined.
>>  hello\t\n\vworld\n\n\n
>>or
>> hello \n world\n\n\n

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

I guess you're right.  When reading Mr. Edelson's question, I read the
\t and \v and \n as the actual characters instead of the escape sequences.
For some reason, I didn't make this mistake on the \n in the format string.
However, this was not my only mistake.

>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 ...  the str(...) macro should be expanded. \t and \v should
>still be escape sequences at this point because in step 5

Yes.

>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).

Yes.

>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).

Sorry, I hadn't read that section of the manual for a while.
(In OO terminology, my mouth now "has-a" foot, though not "is-a" foot.  :-)
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.