10 Feb 2001 johnm   » (Journeyer)

I looked at the calendar today, and it turns out that I've been back from New Zealand for four weeks now. That's hard to believe: I haven't achieved a great deal in all that time, and it sucks to be back.

There's an interesting post on the binutils list about printf as a macro:

printf ("%*s",
#ifdef BFD64
        16,
#else
        8,
#endif
        "");

If printf is a macro, this is nonportable because the preprocessor is not required to be able to handle directives while it's reading the arguments in a macro invocation (ISO/IEC 9899:1999 6.10.3 para 11). In particular, current GCC doesn't handle it.

But what kind of freak would define printf as a macro?

It turns out that doing that is valid: (ibid 7.1.3, and the same language is in C89 too)

[U]nless explicitly stated otherwise [for that function] [...] Any function declared in a header may be additionally implemented as a function-like macro defined in the header

Perhaps it's just me, but I find that surprising. I knew simple things like abs() were allowed to be macros and expected them to be, but I didn't realise the implementer was allowed to provide macro versions of pretty much everything. I'm sure I've written code like the above in the past (or maybe it was just with my own functions, but I don't think so).

The obvious fix is to write the whole printf twice. There's two other solutions too: one cute (but possibly hard on moronic maintainence programmers), and the other surprising at first that it is acceptable, but not when you think about it. IMHO.

The nice thing is that the standard actually tells you all about these solutions. That's unusually friendly for the C standard. :-)

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!