Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you expand #define's into string literals?

Is there a way to get the C++ pre-processor to expand a #define'ed value into a string literal?
for example:

#define NEW_LINE '\n'
Printf("OutputNEW_LINE"); //or whatever

This looks to me like it should be possible as it's before compilation?
Or is there a better design pattern to achieve this kind of behaviour (without resorting to runtime fixes like sprintf)?

EDIT I understand that #define's can be evil, but for arguments sake...

ADDITIONAL Does anyone have any criticism of this approach?

like image 443
Adam Naylor Avatar asked Jul 03 '09 11:07

Adam Naylor


People also ask

Can you expand on this meaning?

Definition of expand on/upon : to speak or write about (something) in a more complete or detailed way : elaborate on She declined to expand on her earlier statement. Please expand on that idea. Researchers will expand upon their data in a new study.

Do you want to expand meaning?

to give more details about something you have said or written: I wonder if you would like to expand on your earlier remarks.

Can be expanded upon?

to give more details about something said or written: He studied with Sigmund Freud and expanded upon many of his theories. Want to learn more?

What is the sentence of Expand?

He has expanded his business to serve the entire state. There are plans to expand the airport. The police have decided to expand their investigation. She plans to expand the lecture series into a book.


1 Answers

This will do it:

#define NEW_LINE "\n"         // Note double quotes
Printf("Output" NEW_LINE);

(Technically it's the compiler joining the strings rather than the preprocessor, but the end result is the same.)

like image 144
RichieHindle Avatar answered Sep 21 '22 19:09

RichieHindle