473,770 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1457
ludov...@techem ail.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.goo glegroups.com.. .
ludov...@techem ail.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******@techem ail.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.goo glegroups.com.. .
ludov...@techem ail.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
2050
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
2145
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
9131
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
5511
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: #if TEST
12
8246
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 the preprocessor to detect that ui32 already exists, when preprocessing F.c? The idea is that F.c will typedef ui32 as above if and only if such typedef is not already in some include file used by F.c.
21
4981
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
2163
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 - once from a relative path, and once from an absolute one - you see that the second inclusion indeed occurs (enters the file and leaves immediately due to the include guard). Why does this happen (I have my speculations, but I want some...
5
6299
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 directive in the middle of macro arguments, which comes handy e.g. to
40
1790
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 done. I want to ask ahead of time because what I do might work but it might not be the "correct" or standard way. #include <stdio.h> #include <stdlib.h> #ifdef LINUX #include <unistd.h>
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10254
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.