Single quoting in preprocessing directives

L

ludovicd

Hi,

I look for a way a put the argument of a fonction between single
quotes within a preprocessor directive such as:

#define fct(v) 'v' (or something equivalent)

fct(\n);


Is there any way to where it would be possible? I tried to use an
escape sequence in the definition of the macro, but that is apparently
illegal.


Thank you for your knowledge,


Ludovic
 
G

Guest

Hi,

I look for a way a put the argument of a fonction between single
quotes within a preprocessor directive such as:

#define fct(v) 'v' (or something equivalent)

fct(\n);


Is there any way to where it would be possible? I tried to use an
escape sequence in the definition of the macro, but that is apparently
illegal.

Sorry, this is not possible. There is a way to do it for "v", but not
for 'v'. However, depending on your needs, "v"[0] may be good enough:

#define fct(v) #v[0]

This expands to an expression of type char with the value of 'v'.
Unlike 'v', it is not of type int. Unlike 'v', it may not be used
anywhere C requires a constant expression. However, it's the best you
can get in standard C.
 
S

Serve Laurijssen

Harald van D?k said:
#define fct(v) 'v' (or something equivalent)

fct(\n);


Is there any way to where it would be possible? I tried to use an
escape sequence in the definition of the macro, but that is apparently
illegal.

Sorry, this is not possible. There is a way to do it for "v", but not
for 'v'. However, depending on your needs, "v"[0] may be good enough:

#define fct(v) #v[0]

This expands to an expression of type char with the value of 'v'.
Unlike 'v', it is not of type int.

if you really want int

#define fcv(v) ((int)(#v[0]))

although it may hide programmer errors in this case
 
I

Ian Collins

Hi,

I look for a way a put the argument of a fonction between single
quotes within a preprocessor directive such as:

#define fct(v) 'v' (or something equivalent)

fct(\n);
What do you want to do with this? Would an inline function serve the
same purpose?
 
G

Guest

Serve said:
Harald van D?k said:
#define fct(v) 'v' (or something equivalent)

fct(\n);


Is there any way to where it would be possible? I tried to use an
escape sequence in the definition of the macro, but that is apparently
illegal.

Sorry, this is not possible. There is a way to do it for "v", but not
for 'v'. However, depending on your needs, "v"[0] may be good enough:

#define fct(v) #v[0]

This expands to an expression of type char with the value of 'v'.
Unlike 'v', it is not of type int.

if you really want int

#define fcv(v) ((int)(#v[0]))

although it may hide programmer errors in this case

I didn't add enough information. The reason why it not being of type
int matters is because programs that are not meant to be 100% portable
may rely on multi-character constants.

fcv(ab) != 'ab'

A cast won't fix this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top