Danvil
Danvil

Reputation: 23031

Newlines in a macro with #

Why are newlines, spaces and tabs ignored by # in a macro and replaced by exactly one space?

Is there a way to get theses white spaces into the string generated by #?

Example:

#include <iostream>
using namespace std;

#define TOSTR(s) #s

int main() {
    cout <<
TOSTR(a newline at the end please
three   spaces
        tab first
) << endl;
}

Output:

a newline at the end please three spaces tab first

(There are no newlines, no three spaces and no tab)

Upvotes: 0

Views: 256

Answers (1)

Smurker
Smurker

Reputation: 714

You'll have to write the newline \n or tab \t character

Upvotes: 2

Related Questions