Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #388514
| Path | csiph.com!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: how to make a macro work as a single line if stmt without braces |
| Date | Tue, 24 Sep 2024 06:18:08 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 74 |
| Message-ID | <86msjx6vhr.fsf@linuxsc.com> (permalink) |
| References | <PaWdnZ3R-9zI6nP7nZ2dnZfqn_GdnZ2d@brightview.co.uk> <pan$b0611$9f7137f3$1d714134$ee77076d@invalid.invalid> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Date | Tue, 24 Sep 2024 15:18:10 +0200 (CEST) |
| Injection-Info | dont-email.me; posting-host="601554cda56817d39319d039c1b5e785"; logging-data="3387589"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ahdx+wLPdSnnFoOXDRoyya6bD9oaBTko=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:eT+q/3rFlSJ7T1u4jR5yqQpQM8I= sha1:XDep7Zxwq62PPh+suN/kihEljhk= |
| Xref | csiph.com comp.lang.c:388514 |
Show key headers only | View raw
Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> writes:
> 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).
The alternative below works without needing __VA_OPT__, in earlier
C standards including C90.
#include <stdarg.h>
#include <stdio.h>
#define WARN(...) (warning_with_origin( __FILE__, __LINE__, __VA_ARGS__ ))
int
warning_with_origin( const char *file, long line, const char *format, ... ){
va_list ap;
int a = (va_start( ap, format ), 0);
int b = a < 0 ? a : fprintf( stderr, "%s#%ld: ", file, line );
int c = b < 0 ? b : vfprintf( stderr, format, ap );
int d = c < 0 ? c : fprintf( stderr, "\n" );
return va_end( ap ), d < 0 ? d : a + b + c + d;
}
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar
how to make a macro work as a single line if stmt without braces Mark Summerfield <mark@qtrac.eu> - 2024-09-21 07:35 +0000
Re: how to make a macro work as a single line if stmt without braces Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-09-21 08:10 +0000
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-21 10:47 +0200
Re: how to make a macro work as a single line if stmt without braces Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-21 12:27 -0700
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-21 21:11 +0100
Re: how to make a macro work as a single line if stmt without braces Opus <ifonly@youknew.org> - 2024-09-21 22:39 +0200
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-21 14:54 -0700
Re: how to make a macro work as a single line if stmt without braces Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-21 20:05 -0700
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-21 21:33 -0700
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-22 14:15 +0200
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-27 03:38 -0700
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-22 13:59 +0200
Re: how to make a macro work as a single line if stmt without braces Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-22 15:11 +0000
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-22 16:56 +0100
Re: how to make a macro work as a single line if stmt without braces Michael S <already5chosen@yahoo.com> - 2024-09-22 19:27 +0300
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-22 17:47 +0100
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-24 07:36 -0700
Re: how to make a macro work as a single line if stmt without braces Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-24 07:52 -0700
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-24 17:24 +0200
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-24 17:22 +0100
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-24 12:28 -0700
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-27 01:43 -0700
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-24 17:35 +0100
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-24 12:30 -0700
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-24 20:42 +0100
Re: how to make a macro work as a single line if stmt without braces Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-24 19:59 +0000
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-24 21:12 +0100
Re: how to make a macro work as a single line if stmt without braces Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-24 22:36 +0000
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-22 13:39 -0700
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-23 08:16 +0200
Re: how to make a macro work as a single line if stmt without braces Richard Harnden <richard.nospam@gmail.invalid> - 2024-09-23 08:06 +0100
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-23 11:14 +0200
Re: how to make a macro work as a single line if stmt without braces Richard Harnden <richard.nospam@gmail.invalid> - 2024-09-23 11:37 +0100
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-23 14:22 +0200
Modern text editor - 'bout time someone paid attention to keeping the thread title relevant!!! (Was: how to make a macro work as a single line if stmt without braces) gazelle@shell.xmission.com (Kenny McCormack) - 2024-09-22 16:03 +0000
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-22 18:03 +0200
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-28 05:02 -0700
Re: how to make a macro work as a single line if stmt without braces Michael S <already5chosen@yahoo.com> - 2024-09-28 20:30 +0300
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-28 21:53 -0700
Re: how to make a macro work as a single line if stmt without braces Michael S <already5chosen@yahoo.com> - 2024-09-29 12:48 +0300
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-29 19:18 -0700
Re: how to make a macro work as a single line if stmt without braces Alan Mackenzie <acm@muc.de> - 2024-09-30 15:05 +0000
Re: how to make a macro work as a single line if stmt without braces Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-28 21:02 -0700
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-28 22:47 -0700
Re: how to make a macro work as a single line if stmt without braces Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-29 07:41 -0700
Re: how to make a macro work as a single line if stmt without braces Michael S <already5chosen@yahoo.com> - 2024-09-29 18:04 +0300
Re: how to make a macro work as a single line if stmt without braces scott@slp53.sl.home (Scott Lurndal) - 2024-09-29 18:30 +0000
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-29 16:39 -0700
Re: how to make a macro work as a single line if stmt without braces Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-30 02:29 +0000
Re: how to make a macro work as a single line if stmt without braces Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-09-30 11:26 +0200
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-29 18:39 -0700
Re: how to make a macro work as a single line if stmt without braces Kaz Kylheku <643-408-1753@kylheku.com> - 2024-09-30 02:03 +0000
Re: how to make a macro work as a single line if stmt without braces David Brown <david.brown@hesbynett.no> - 2024-09-30 13:17 +0200
Re: how to make a macro work as a single line if stmt without braces Bart <bc@freeuk.com> - 2024-09-21 10:13 +0100
Re: how to make a macro work as a single line if stmt without braces Mark Summerfield <mark@qtrac.eu> - 2024-09-21 09:53 +0000
Re: how to make a macro work as a single line if stmt without braces Ike Naar <ike@sdf.org> - 2024-09-21 09:15 +0000
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-21 14:44 -0700
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-21 06:07 -0700
Re: how to make a macro work as a single line if stmt without braces Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-09-21 15:55 -0700
Re: how to make a macro work as a single line if stmt without braces Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-21 12:17 -0700
Re: how to make a macro work as a single line if stmt without braces Blue-Maned_Hawk <bluemanedhawk@invalid.invalid> - 2024-09-21 21:18 +0000
Re: how to make a macro work as a single line if stmt without braces Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-24 06:18 -0700
csiph-web