Adding Quotes to Argument in C++ Preprocessor

Macro with double quotes

You want this:

#define DISP(f,x) printf(#f"(%g) = %g\n", x, f(x))

that provides the following output:

sqrt(3) = 1.73205

(See http://codepad.org/hX96Leta)

defining C++ preprocessor macro with quotation marks

You can use the stringizing operator, #:

#define PE(x) std::cout << #x << std::endl;

I would suggest that you remove the semicolon from your macro, though. So,

#define PE(x) std::cout << #x << std::endl
...
PE(hello);

Two double quotes around preprocessor token in macro definition

They’re unnecessary. Whoever wrote the code probably intended to use the fact that you the preprocessor concatenates successive C string literals into one literal so that e.g. S("hi") results in "" "hi" ""_s, which, in turn, results in "hi"_s.

However, there is no need to have the first "" in the macro definition. And there’s no need for the second "" either, since we can use the token pasting operator ## instead:

#define S(str) str ## _s

This has the exact same effect, and is idiomatic.

How to single-quote an argument in a macro?

The best you can do is

#define Q(x) ((#x)[0])

or

#define SINGLEQUOTED_A 'A'
#define SINGLEQUOTED_B 'B'
...
#define SINGLEQUOTED_z 'z'

#define Q(x) SINGLEQUOTED_##x

This only works for a-z, A-Z, 0-9 and _ (and $ for some compilers).

Return quoted string in C macro

Use #sequence but add the quotes before and after:

#define macro(sequence) "\"" #sequence "\""

The string literals will be concatenated giving you the result you want.

For example:

#define hello abc
printf("%s\n", macro(hello));

Will print "hello" (including the quotes).

Do preprocessors expand macros surrounded by quotation marks?

Do common preprocessors like cpp and fpp expand macros surrounded by a
pair of quotation marks?

The C language specification describes the behavior of conforming C preprocessors. The actual standards for C are not freely available, but you can get copies of late drafts. For C18, for example, you could refer to N2176. In particular, you should have a look at sections 5.1.1.2 and 6.10.3. Of particular relevance is footnote 173 in section 6.10.3:

Since, by macro-replacement time, all character constants and string
literals are preprocessing tokens, not sequences possibly containing
identifier-like subsequences (see 5.1.1.2, translation phases), they
are never scanned for macro names or parameters.

(Substantially the same text appears in earlier versions of the standard, too.)

The bottom line for C, then, is that no, a conforming C preprocessor does not perform macro replacement on the contents of string literals or character constants.


The situation for Fortran is less clear cut, because the Fortran language specification does not define preprocessing facilities. There is an include statement built in to the language itself, but Fortran practitioners generally would not consider its use to involve preprocessing. Fortran source code rarely relies on preprocessing features such as macro expansion or conditional compilation.

Some Fortran implementations nevertheless do provide a preprocessing facility, sometimes available as a standalone program named fpp. You would need to consult the documentation of your specific fpp for details, but generally these are adaptations of the C preprocessor to Fortran syntax. As such, no, I would not expect a Fortran preprocessor to perform macro expansion on the contents of character literals. I am not aware of any implementations that defy my expectations in that regard.

C/C++ How to expand a macro parameter into text between quotes

Don't put #fmt in the quotes. Just use string literal concatenation to join the two literals.

#define LOG(fmt, ...) message("%s %s(): " fmt, __FILE__, __func__, __VA_ARGS__);


Related Topics



Leave a reply



Submit