Deutsch   English   Français   Italiano  
<pan$b0611$9f7137f3$1d714134$ee77076d@invalid.invalid>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: Blue-Maned_Hawk <bluemanedhawk@invalid.invalid>
Newsgroups: comp.lang.c
Subject: Re: how to make a macro work as a single line if stmt without braces
Date: Sat, 21 Sep 2024 21:18:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <pan$b0611$9f7137f3$1d714134$ee77076d@invalid.invalid>
References: <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 21 Sep 2024 23:18:07 +0200 (CEST)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="30fb21e54b60a514fc4a34f14ba29187";
	logging-data="1834537"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18KmpESDclV0bjXP4sLZaLRRj7LVdUDITY="
User-Agent: Pan/0.154 (Izium; 517acf4)
Cancel-Lock: sha1:HwK7FHOi5kynjr1jqCmDS0cSMTw=
X-Face: Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronuku
 pokaiwhenuakitanatahu
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAIAAADYYG7QAAACh0lEQVRYw71Z21bD
 MAzzevbfkr4cHjrSXJyL044+MDa6WLEl2SkvkrZ1AbAvXO+bUGSCPYnsuIVGMpm
 ZLnjX718GhAKNsp8lON2F9VrhELwIgJlBepkZjA78rVK+FkmNhEJK76UsJlz8+E
 rJsjrpYouhLo/SC6qPHgakFOR8wV9+8rCfO/I/oVnmUZUp42/LW2XkLj9TCFNM9
 jp5g2EmHZgpYZjCOkYU7sXVogRylJqpdggoFLG1g09Flah/7kErCxzR9HgXPYsq
 0glb9cxjIz2Vsk9AmAoCSxECpD713joMKjQqLAtmMqJmXjdVvlMnMQCVITotJd1
 z+fh1f1NNo+vuc1KnhWUmY7t03vydTud9BbXCtN3L2PL3bK7JCNG0GHzuZxafyB
 fxevCxpm1vrwZltqw6SILCcdoCE6PGQC8wZWDA9Or7Qp5s3lAZezys0nDazs9S9
 R0TjwEiksRxLkNPC1NMMWPs1bj0Ei0Yuo+JVtFLuzP1NRJ16qXWN8DhhtmS4PDg
 O6mqRxs4bEJrYt087mSIow/1VzW2oFlMQuiuIy/KsUagvhdw6hSjJGlIavbLF8x
 j3X47bccLcUSi0dkWh1nUZNhANT1tHKUXrNxNLbd9KPb9wDDVrKwmPQMOPQ1oy6
 k5I1DwzDeRJd3jVIhDAUxq3ngzJG4CCkNXZxZVMcjefoK2J0gUY2S3rxz/RuTFx
 2zHd9U+obimJXMG4edsk/2j5pTU5G1MmzbRLxkfq5EiT1GGsidvMGzi+1goGb2l
 GCrN+nGnV8xj3q3JLRDVPL96vUc7Z4aJ3TN1mVqWAMJMfG+Jxh6TQqP+92iZkCU
 xtglds1AB6r0aiSHKcnFck+p/c/0CbacFLQcajGcAAAAASUVORK5CYII=
Bytes: 4038

Mark Summerfield wrote:

> I have this macro:
> 
> #define WARN(...)                                       \
>     do {                                                \
>         fprintf(stderr, "%s#%d: ", __FILE__, __LINE__); \
>         fprintf(stderr, __VA_ARGS__);                   \
>     } while (0);
> 
> which I use like this:
> 
> total++;
> if (failed) {
>     WARN("failed because...");
> } else
>     ok++;
> 
> I would prefer to be able to write this instead:
> 
> total++;
> if (failed)
>     WARN("failed because...");
> else
>     ok++;
> 
> but doing so results in a compiler error:
> 
> error: 'else' without a previous 'if'

Since the initial problem has already been solved by other messages, 
here's some extra completely unsolicited advice that you are free to 
ignore:

If you intend for this macro to be invoked like how one would invoke the 
printf function (i.e. the arguments consisting of a format string literal 
followed by arguments to be formatted into it), you could get away with 
defining it like this instead:

#define WARN(msg, ...) (fprintf(stderr, "%s#%d: " msg, __FILE__, __LINE__ 
__VA_OPT__(,) __VA_ARGS__))

This would break if you try to invoke it with a nonliteral string, because 
this takes advantage of preprocessor adjacent string literal 
concatenation, but modern compilers tend to complain mightily if you try 
that anyway.  It would have the advantage of expanding to an expression 
instead, which is often more beneficial for macros because it permits more 
flexibility in placement (something that i find unlikely to be relevant 
for this particular one, but which can't really particularly hurt to have 
anyway).

-- 
Blue-Maned_Hawk│shortens to Hawk│/blu.mɛin.dʰak/│he/him/his/himself/Mr.
blue-maned_hawk.srht.site
How do i run in these marrowy clogs?