using defined characters as strings

M

Martijn

Hi,

I think I can best ask my question by example. I may be overlooking
something, but this is like what I have:

#define ID1 'i'
#define ID2 'd'
#define ID3 'n'

This is what I want in my output file:

i=100:d=200:n=300

This is what I do:

sprintf("%c=%d:%c=%d:%c=%d\n",
ID1, 100,
ID2, 200,
ID3, 300);

That means that the characters are inserted run-time. Is there a way to
make them part of the string-constant so that I can put them into my string
compile-time?

Sorry if the question is formulated somewhat strange :) Thanks for any
help!
 
M

Michael Mair

Martijn said:
Hi,

I think I can best ask my question by example. I may be overlooking
something, but this is like what I have:

#define ID1 'i'
#define ID2 'd'
#define ID3 'n'

This is what I want in my output file:

i=100:d=200:n=300

This is what I do:

sprintf("%c=%d:%c=%d:%c=%d\n",

You either forgot the buffer or use the wrong *printf() function.
ID1, 100,
ID2, 200,
ID3, 300);

That means that the characters are inserted run-time. Is there a way to
make them part of the string-constant so that I can put them into my string
compile-time?

Sorry if the question is formulated somewhat strange :) Thanks for any
help!

Does
#define STRINGIZE(S) #S
#define XSTR(S) STRINGIZE(S)

#define ID1 i

....
printf(XSTR(ID1)"=%d",100)

help you or do you need the character constants elsewhere?


Cheers
Michael
 
C

CBFalconer

Martijn said:
Hi,

I think I can best ask my question by example. I may be overlooking
something, but this is like what I have:

#define ID1 'i'
#define ID2 'd'
#define ID3 'n'

This is what I want in my output file:

i=100:d=200:n=300

This is what I do:

sprintf("%c=%d:%c=%d:%c=%d\n",
ID1, 100,
ID2, 200,
ID3, 300);

That means that the characters are inserted run-time. Is there a way to
make them part of the string-constant so that I can put them into my string
compile-time?

sprintf("i=%d:d=%d:n=%d\n", 100, 200, 300);
 
B

Barry Schwarz

Hi,

I think I can best ask my question by example. I may be overlooking
something, but this is like what I have:

#define ID1 'i'
#define ID2 'd'
#define ID3 'n'

This is what I want in my output file:

i=100:d=200:n=300

This is what I do:

sprintf("%c=%d:%c=%d:%c=%d\n",
ID1, 100,
ID2, 200,
ID3, 300);

That means that the characters are inserted run-time. Is there a way to
make them part of the string-constant so that I can put them into my string
compile-time?

If you change the defines to specify double quotes (") instead of
single quotes ('), then your format string can be coded as
ID1 "=%d:" ID2 "=%d:" ID3 "=%d\n"
since the compiler will automatically merge adjacent quoted strings
together.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,081
Latest member
AnyaMerry

Latest Threads

Top