From: Richard Harvey Chapman <hchapman-gcc-help@3gfp.com>
To: gcc-help@gcc.gnu.org
Subject: Macro question regarding single quotes
Date: Sat, 16 Aug 2008 11:38:00 -0000 [thread overview]
Message-ID: <48A5E959.6050505@3gfp.com> (raw)
I'd like to shorten-up so redundant code in a switch statement by using
a macro for each case. The following code illustrates the problem. In
short, is there a way to write the PARSE(a,b) macro that will work in
the manner shown below?
Thanks,
H.
========
#include <stdio.h>
int varAB = 1;
int varFG = 2;
#define TEST1(a,b) printf("'%c' '%c' = %d\n", *#a, *#b, var##a##b)
#define TEST2(a,b) printf("'%c' '%c' = %d\n", #a[0], #b[0], var##a##b)
int ParseAB(void) { return(0); }
int ParseCD(void) { return(0); }
int ParseEF(void) { return(0); }
// * not allowed in case statement
#define PARSE(a,b) case (*#a << 8) | *#b: \
status = Parse##a##b(); \
break
// ends up being literally 'a' and 'b'
#define PARSE2(a,b) case ('a' << 8) | 'b': \
status = Parse##a##b(); \
break
int main()
{
int status = 0;
TEST1(A,B);
TEST2(F,G);
switch (('A' << 8) | 'B')
{
// This is what the output of the PARSE macro should be:
case ('A' << 8) | 'B':
status = ParseAB();
break;
// This is how I'd like to use the macro.
PARSE(C,D);
PARSE(E,F);
default:
status = -1;
}
return(0);
}
next reply other threads:[~2008-08-15 20:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-16 11:38 Richard Harvey Chapman [this message]
2008-08-21 13:35 ` Bernd Jendrissek
2008-08-21 13:43 ` Richard Harvey Chapman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=48A5E959.6050505@3gfp.com \
--to=hchapman-gcc-help@3gfp.com \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).