473,386 Members | 1,606 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,386 software developers and data experts.

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

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 5052
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 Expert 1GB
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
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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
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,...

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.