Shifa Rizwan BS(CS) 2025 FAST NU LHR
last year
Shifacountry asked

What is the difference between writing \n before a string and after a string in C++?

Abhay Jajodia
Expert
last year
Abhay Jajodia answered

Hi Shifa,

\n means new line.

  • If you put it before the text, it moves to a new line first, then prints the text.

    cout << "\nHello";
    
  • If you put it after the text, it prints the text first, then moves to the next line.

    cout << "Hello\n";
    

So the difference is whether the line break happens before the message or after it.

If you have more questions, I am here to help 😊

C++
This question was asked as part of the Learn C++ Basics course.