* Macro question regarding single quotes
@ 2008-08-16 11:38 Richard Harvey Chapman
2008-08-21 13:35 ` Bernd Jendrissek
0 siblings, 1 reply; 3+ messages in thread
From: Richard Harvey Chapman @ 2008-08-16 11:38 UTC (permalink / raw)
To: gcc-help
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);
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Macro question regarding single quotes
2008-08-16 11:38 Macro question regarding single quotes Richard Harvey Chapman
@ 2008-08-21 13:35 ` Bernd Jendrissek
2008-08-21 13:43 ` Richard Harvey Chapman
0 siblings, 1 reply; 3+ messages in thread
From: Bernd Jendrissek @ 2008-08-21 13:35 UTC (permalink / raw)
To: Richard Harvey Chapman; +Cc: gcc-help
On Fri, Aug 15, 2008 at 10:38 PM, Richard Harvey Chapman
<hchapman-gcc-help@3gfp.com> wrote:
> 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?
How about something like this:
#define rhc__A 'A'
#define rhc__B 'B'
#define rhc__C 'C'
#define rhc__D 'D'
#define rhc__E 'E'
#define rhc__F 'F'
// etc.
// * not allowed in case statement
#define PARSE(a,b) case (rhc__##a << 8) | rhc__##b: \
status = Parse##a##b(); \
break
This depends on a reasonable-sized alphabet, of course!
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Macro question regarding single quotes
2008-08-21 13:35 ` Bernd Jendrissek
@ 2008-08-21 13:43 ` Richard Harvey Chapman
0 siblings, 0 replies; 3+ messages in thread
From: Richard Harvey Chapman @ 2008-08-21 13:43 UTC (permalink / raw)
To: gcc
Perfect! And so simple.
Thanks,
H.
Bernd Jendrissek wrote:
> On Fri, Aug 15, 2008 at 10:38 PM, Richard Harvey Chapman
> <hchapman-gcc-help@3gfp.com> wrote:
>> 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?
>
> How about something like this:
>
> #define rhc__A 'A'
> #define rhc__B 'B'
> #define rhc__C 'C'
> #define rhc__D 'D'
> #define rhc__E 'E'
> #define rhc__F 'F'
> // etc.
>
> // * not allowed in case statement
> #define PARSE(a,b) case (rhc__##a << 8) | rhc__##b: \
> status = Parse##a##b(); \
> break
>
> This depends on a reasonable-sized alphabet, of course!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-08-21 13:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-16 11:38 Macro question regarding single quotes Richard Harvey Chapman
2008-08-21 13:35 ` Bernd Jendrissek
2008-08-21 13:43 ` Richard Harvey Chapman
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).