468,484 Members | 1,675 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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 4885
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

Post your reply

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

Similar topics

14 posts views Thread by Alex | last post: by
3 posts views Thread by lovecreatesbeauty | last post: by
8 posts views Thread by Anshul | last post: by
9 posts views Thread by blangela | last post: by
20 posts views Thread by karthikbalaguru | last post: by
2 posts views Thread by Alex Weber | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.