Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 68143 - Last Review: July 5, 2005 - Revision: 2.1
PRB: Stringize Operator Fails to Insert Backslashes
This article was previously published under Q68143
The Microsoft C and C/C++ compilers support the use of the stringizing
preprocessor operator only in macros that take arguments. 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.
This behavior is by design.
Following are some sample macros that use the stringizing 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 for C Compiler version 6.0:
printf("This: \\" prints an escaped double quote mark")
Preprocessor Output for C/C++ Compilers, Versions 7.0 and above:
error C2001: newline in constant
fatal error C1057: unexpected end-of-file in macro expansion
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
Expected Output:
printf("I am printing a quote: \"");
APPLIES TO
- Microsoft Visual C++ 1.0 Professional Edition
- Microsoft Visual C++ 1.5 Professional Edition
- Microsoft Visual C++ 2.0 Professional Edition
- Microsoft Visual C++ 2.1
- Microsoft Visual C++ 4.0 Standard Edition
- Microsoft Visual C++ 5.0 Enterprise Edition
- Microsoft Visual C++ 6.0 Enterprise Edition
- Microsoft Visual C++ 5.0 Professional Edition
- Microsoft Visual C++ 6.0 Professional Edition
- Microsoft Visual C++, 32-bit Learning Edition 6.0
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please