473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Single quoting in preprocessing directives


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

Feb 10 '07 #1
4 1433
ludov...@techemail.com wrote:
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.

Feb 10 '07 #2

"Harald van D?k" <tr*****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
ludov...@techemail.com wrote:
>#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
Feb 10 '07 #3
lu******@techemail.com wrote:
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?

--
Ian Collins.
Feb 10 '07 #4
Serve Laurijssen wrote:
"Harald van D?k" <tr*****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
ludov...@techemail.com wrote:
#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.

Feb 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, In standard C++, is it required that preprocessing directives (#include, #if, etc...) start in column 0, or is that just a C thing? Thank, Dave
3
by: Ron | last post by:
Please, consider the following code: const int a = 1; #if a == 1 #define VAR 200 #else #define VAR 100 #endif int main() {
16
by: Puneet | last post by:
Hi ALL, I have a silly question... (may be) Can we write a single line C program whose output is the program itself? Is anybody know the answer please tell me. Puneet
5
by: cody | last post by:
the following leads to an error (note that TEST is not defined): #if TEST string s = @" #"; // <-- the error is "preprocessing directive expected" #endif also, here we get the same error: ...
12
by: Thomas Carter | last post by:
Imagine that there is some include file f.h that contains the following line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it possible for...
21
by: Marius Lazer | last post by:
Is it possible to write a macro that single-quotes its argument? #define SOME_MACRO(x) such that SOME_MACRO(foo) expands to 'foo' Thanks, Marius
18
by: gutmant | last post by:
Say you have a file, a.h with an include guard. If you include it twice and look at the preprocessed output, you see there's no sign for the second inclusion. However, if you include it twice -...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
40
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.