Q32303: Assert Macro Anomaly; Generating Syntax Errors
Article: Q32303
Product(s): See article
Version(s): 5.10 | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 5-JUL-1988
The following code demonstrates an anomaly with the C Version 5.10
compiler when compiled with the default options. Compile-time syntax
warnings are generated when there are no apparent syntax errors.
#include <stdio.h>
#include <assert.h>
main()
{
if(1)
assert(1+1);
else
assert(1+2);
}
Response:
The syntax errors are generated because assert() is implemented as
a macro and is expanded to the following form:
if (!(1+1)) { \
fprintf(stderr, _assertstring, #1+1, __FILE__, __LINE__); \
fflush(stderr); \
abort(); \
} \
}
When this macro is placed inside an if else conditional, the
closing brace of the macro is seen as a syntax error.
Although the syntax for the if else conditional in the above
example is legal, it does not make much sense for use with assert
because assert will call the abort function if it is true. Therefore,
you can accomplish the same results with the following program:
#include <stdio.h>
#include <assert.h>
main()
{
if(1)
assert(1+1);
assert(1+2);
}
Microsoft is researching this problem and will post new information
as it becomes available.
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1986-2002.