C++ New Lines
In C++, the newline character is represented by the '\n' escape sequence. You can use this character in a string to create a new line in the output.
![]() |
C++ New Lines With Example |
Here is an example of using the newline character in 'cout':
#include <iostream>
int main() {
std::cout << "Line 1\nLine 2\nLine 3" << std::endl;
return 0;
}
This will output the following text to the console:
Line 1
Line 2
Line 3
You can also use std::endl to insert a newline at the end of the output.
#include <iostream>
int main() {
std::cout << "Line 1" << std::endl;
std::cout << "Line 2" << std::endl;
std::cout << "Line 3" << std::endl;
return 0;
}
This will also output the same text as above.
You can use printf() or puts() function to print the newline as well.
printf("Line 1\nLine 2\nLine 3");
or
puts("Line 1");
puts("Line 2");
puts("Line 3");
Both will give the same output as above.
Post a Comment