Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: A variation of # |
| Date | 2011-11-07 15:07 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <4EB83A82.1080100@verizon.net> (permalink) |
| References | <8f2cadb2-614d-4754-8bc3-bb7c063524ae@c16g2000pre.googlegroups.com> <9hqo41F4bgU2@mid.individual.net> <73822171-7a92-4420-9eea-e74bbeda1ff8@x28g2000prb.googlegroups.com> |
On 11/07/2011 02:11 PM, Andrey Vul wrote:
> On Nov 7, 1:59 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> On 11/ 8/11 07:53 AM, Andrey Vul wrote:
>>
>>> To stringify pp-constants, we use #foo notation.
>>> Is there a method of charifying constants defined on the preprocessor
>>> level, assuming the precondition of length being 1 is true?
I presume there's some reason why the constants cannot, themselves, be
defined as character constants?
>>> That is, does there exist a pp-builtin CHARIFY such that CHARIFY(x) =
>>> 'x' ?
>>
>> Something like
>>
>> #define CHAR(x) #x [0]
>>
>
> Can't be used inside switch.
There's no simple way I can think of to define a macro that generates a
character constant whose value depends upon the expansion of another
macro. I did, however, manage to come up with this ugly monstrosity,
which cannot possibly qualify as "simple", which I've inserted into some
test code:
#include <stdlib.h>
#define TRICAT(a, b, c) a ## b ## c
#define CHAR(a, b, c) TRICAT(a, b, c)
#define CASE1 x
int main(int argc, char *argv[])
{
if(argc <= 1)
return EXIT_FAILURE;
switch (*argv[1])
{
case CHAR('
, CASE1, '
): break;
default:
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
The key thing that makes this work is that ' immediately followed by a
newline is not recognized as any other type of pre-processing token, so
it counts as a preprocessing token of it's own, under the classification
"each non-white-space character that cannot be one of the above" (6.4p1).
The two-level expansion allows CASE1 to be expanded to x before being
concatenated with the ' tokens.
If you can convince the relevant person to define the relevant constants
as character constants, that would make things a lot easier.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A variation of # Andrey Vul <andrey.vul@gmail.com> - 2011-11-07 10:53 -0800
Re: A variation of # Ian Collins <ian-news@hotmail.com> - 2011-11-08 07:59 +1300
Re: A variation of # Andrey Vul <andrey.vul@gmail.com> - 2011-11-07 11:11 -0800
Re: A variation of # James Kuyper <jameskuyper@verizon.net> - 2011-11-07 15:07 -0500
Re: A variation of # lawrence.jones@siemens.com - 2011-11-07 17:53 -0500
Re: A variation of # James Kuyper <jameskuyper@verizon.net> - 2011-11-07 18:31 -0500
Re: A variation of # lawrence.jones@siemens.com - 2011-11-08 09:16 -0500
Re: A variation of # Thad Smith <ThadSmith@acm.org> - 2011-11-07 19:51 -0700
Re: A variation of # Harald van Dijk <truedfx@gmail.com> - 2011-11-08 09:01 -0800
Re: A variation of # James Kuyper <jameskuyper@verizon.net> - 2011-11-08 12:15 -0500
Re: A variation of # Andrey Vul <andrey.vul@gmail.com> - 2011-11-07 17:32 -0800
Re: A variation of # James Kuyper <jameskuyper@verizon.net> - 2011-11-07 21:17 -0500
Re: A variation of # Andrey Vul <andrey.vul@gmail.com> - 2011-11-07 11:08 -0800
Re: A variation of # Peter Nilsson <airia@acay.com.au> - 2011-11-07 16:46 -0800
csiph-web