assert

Doug Gwyn gwyn at brl-smoke.ARPA
Sun Mar 15 06:39:22 AEST 1987


In article <4907 at brl-adm.ARPA> Dizio at udel.edu writes:
>#define assert(EX) if (EX) ; else _assert("EX", __FILE__, __LINE__)
>                                          ^culprit
>It fails when given given an expression containing '"'.  
>  eg. assert ((fp = fopen("filename","r")) != NULL)
>
>My question is this.  Is this a proper implementation of this
>functionality.  I'm not sure why it wants to pass the EX to
>_assert except perhaps to print the expression out,  but in reality
>the line number and file name is all I ever need to know.

The problem is that there is no way in K&R C to construct
a string literal with a macro argument embedded in it.
When Reiser implemented his preprocessor (used by every
version of UNIX that I have seen so far), he supported
this so that macros such as assert() would work (except
for cases such as the one you stumbled across).  The
CTRL() macro one often encounters uses a similar trick
to construct a character constant.

X3J11 recognized the need for some such facility, but
could not bring themselves to bless this Reiserism,
which conflicts with much existing practice on non-UNIX
systems.  They therefore introduced a "stringize" operator
(but not a "charize" one, alas).  I don't recall if the
Draft Standard points out the need to escape " characters
in the macro argument when stringizing, but I think it
does (I left my copy at home).



More information about the Comp.lang.c mailing list