PRB: Stringize Operator Fails to Insert Backslashes

ID Number: Q68143

5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

SYMPTOMS

In Microsoft C versions 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version 7.0

the stringizing operator is used only with the arguments of macros.

If a # precedes a formal parameter in the definition of a macro, the

actual argument is enclosed in double quotation marks and treated as

a string when the macro is expanded.

If the argument contains characters that normally must be preceded

by a backslash (\) when appearing in a string (such as " or \), the

backslash should be automatically inserted. However, there are many

cases where the preprocessor fails to do this.

STATUS

This behavior is by design.

More Information:

Following are some sample macros that use the stringize operator. They

are followed by a line showing the incorrect preprocessor output

(compiling with /P) and the output that was expected.

Sample Code

-----------

Example 1:

#define print_filename(x) printf(#x);

main()

{

print_filename(d:\test\sscanf.c)

}

Preprocessor Output:

printf("d:\test\sscanf.c");

Expected Output:

printf("d:\\test\\sscanf.c");

Example 2:

#define print_filename(x) printf(#x);

main()

{

print_filename(This: \" prints an escaped double quote mark)

}

Preprocessor Output:

printf("This: \\" prints an escaped double quote mark")

Expected Output:

printf("This: \\\" prints an escaped double quote mark")

Example 3:

#define print_stuff(x) printf(#x);

main()

{

print_stuff(I am printing a quote: ")

}

Preprocessor Output:

error C2001: newline in constant

fatal error C1057: unexpected end-of-file in macro

expansion (missing ')'?)

Expected Output:

printf("I am printing a quote: \"");

Additional reference words: 5.10 6.00 6.00a 6.00ax