To assign a string that spans multiple lines at once

Asked 1 years ago, Updated 1 years ago, 129 views

I'm just going to copy and paste the sentence from the file. There is an error in my code. And the color changes in the middle What should I do?

using namespace std;

int main(){
    const char* msg = "hello 
    my 
    world!
    ...
    end"
}

c++ string-literals

2022-09-22 22:33

1 Answers

There are two ways.

The compiler will paste it on its own and no spaces will be added between them.

const char *msg =
  "hello"
  "my"
  "world!"
  ...
  "end";

Results:

hello c++ world!

Note that this method adds a space between the strings.

int main(){
    char* msg = "hello \
    c++ \
    world!";

    cout << msg << endl;
}

Results:

hello     c++    world!


2022-09-22 22:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.