473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is it possible to use #define constant integers in string literals?

3 New Member
Hello,

I've been searching for some time now and have been unable to find an answer to my question. Essentially, I would like to be able to do this:
Expand|Select|Wrap|Line Numbers
  1. #define WIDTH 5
  2.  
  3. printf("%" WIDTH "d", some_int);
Of course that doesn't work.

If you have a function-like macro, you can prefix its arguments with '#' and the preprocessor will wrap them with quotation marks (as well as perform escaping and other features.) Is there a way to use the same functionality with object-like macros?


Thanks
Jan 24 '08 #1
3 5053
traynor
3 New Member
I managed to find a solution. I am not sure how it works, but it is the standard way of doing it. The solution comes from an example in the C:1999 standard (PDF, pg. 154) that does exactly what I'm looking for. Here's the excerpt:
Expand|Select|Wrap|Line Numbers
  1. #define hash_hash # ## #
  2. #define mkstr(a) # a
  3. #define in_between(a) mkstr(a)
  4. #define join(c, d) in_between(c hash_hash d)
  5. char p[] = join(x, y); // equivalent to
  6.                        // char p[] = "x ## y";
The in_between() macro is why this works. If you just call mkstr() directly, you get "x hash_hash y". You need to define two function-like macros, one that calls the other, in order to turn an object-like macro into a string literal.
Jan 24 '08 #2
gpraghuram
1,275 Recognized Expert Top Contributor
I managed to find a solution. I am not sure how it works, but it is the standard way of doing it. The solution comes from an example in the C:1999 standard (PDF, pg. 154) that does exactly what I'm looking for. Here's the excerpt:
Expand|Select|Wrap|Line Numbers
  1. #define hash_hash # ## #
  2. #define mkstr(a) # a
  3. #define in_between(a) mkstr(a)
  4. #define join(c, d) in_between(c hash_hash d)
  5. char p[] = join(x, y); // equivalent to
  6.                        // char p[] = "x ## y";
The in_between() macro is why this works. If you just call mkstr() directly, you get "x hash_hash y". You need to define two function-like macros, one that calls the other, in order to turn an object-like macro into a string literal.

Whatever u have used are trigraph sequences.
You have to check whether the current ANSI standard supports this

Raghuram
Jan 24 '08 #3
traynor
3 New Member
Whatever u have used are trigraph sequences.
You have to check whether the current ANSI standard supports this

Raghuram
I don't think you're right. According to the same document, "for both object-like and function-like macro invocations, before the replacement list is reexamined for more macro names to replace, each instance of a ## preprocessing token in the replacement list (not from an argument) is deleted and the preceding preprocessing token is concatenated with the following preprocessing token."

You can get the same effect without using that token:
Expand|Select|Wrap|Line Numbers
  1. #define WIDTH 10
  2. #define mkstr(a) # a
  3. #define in_between(a) mkstr(a)
  4. printf("%" in_between(WIDTH) "d", some_int);
The last line will be processed as 'printf("%" "10" "d", some_int)'.

The only odd token in that example is the '#' before the identifier in the replacement list of 'mkstr()'. The document says "if, in the replacement list, a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument" The trigraph sequence for '#' is '??=', so I am pretty sure that there are no trigraph sequences in that example.
Jan 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Veit Wiessner | last post by:
I wrote a program that handles the buildcount of projects (gets called every time I compile a project, it writes a header file which is #include-ed in the project). My question is this, is it...
15
by: Ramaraj M Bijur | last post by:
Hi All, Could anyone help me to resolve following problem in C the IDE used is Microsoft VC++, Please let me know the which option in VC++ will do the needful The problem statement:...
14
by: Alex | last post by:
Hi, I have a question regarding to string variable. Please look at below example. ------------------------------ #define STR "ABC" char *testStr testStr = STR;
3
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5...
8
by: Anshul | last post by:
Hi, I tried compiling this code - int main() { char abc = "%s"; printf("\033[1m" abc "\033[m" , "doing"); }
9
by: blangela | last post by:
Can somepoint me to a good discussion on the pros and cons of using #define versus a constant variable in your C+ application? Thanks, Bob
4
by: Quentin Yuan | last post by:
I always consider that the constant character strings of which literal value are the same lay out at the same logic address, in another words, every constant character string have only one copy in...
20
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above...
2
by: Alex Weber | last post by:
weird issue i ran into today... its possible to define case-insensitive constants outside the scope of a class using: define('name', 'value', false); however, you cannot use define() for...
0
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,...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.