-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the file new-line-in-cpp.md to Day 2 (#216)
- Loading branch information
1 parent
8d18202
commit c52aac3
Showing
1 changed file
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
sidebar_position: 4 | ||
title: "Adding New Line in C++" | ||
description: "Explore various methods to add new line in C++ with illustrative examples." | ||
sidebar_label: "New Line in C++" | ||
slug: adding-new-line-in-cpp | ||
--- | ||
|
||
|
||
# Adding New Line in C++ | ||
There are two common ways to add a new line in C++: using `"\n"` and `"std::endl"`. | ||
|
||
## Using `"\n"`: | ||
The `"\n"` escape sequence is a simple way to add a new line in C++. It instructs the program to start printing from the next line. | ||
|
||
#### Example: | ||
```cpp | ||
#include <iostream> | ||
using namespace std; | ||
int main() { | ||
cout << "Hello\n"; // Using newline character (\n) | ||
cout << "World\n"; | ||
return 0; | ||
} | ||
``` | ||
|
||
**Output**: | ||
```cpp | ||
Hello | ||
World | ||
``` | ||
|
||
|
||
## Using `"std::endl"`: | ||
`std::endl` is a manipulator in the C++ standard library that not only adds a new - line but also flushes the output buffer, ensuring immediate display. | ||
|
||
#### Example: | ||
```cpp | ||
#include <iostream> | ||
using namespace std; | ||
int main() { | ||
cout << "Hello" << endl; // Using std::endl | ||
cout << "World" << endl; | ||
return 0; | ||
} | ||
``` | ||
**Output**: | ||
```cpp | ||
Hello | ||
World | ||
``` | ||
|
||
|
||
## Comparison of `\n` and `endl`: | ||
Both `"\n"` and `std::endl` produce the same output, but there are differences in their usage and performance. | ||
|
||
#### Performance : | ||
In terms of performance, `\n` is generally faster than `std::endl`. | ||
`std::endl` flushes the output buffer, which may impact performance, especially for large outputs. On the other hand, `\n` does not flush the buffer immediately, making it more efficient. | ||
|
||
#### Usage: | ||
|
||
- Use `std::endl` when you need immediate display of the output or when you want to ensure that certain messages are visible right away, such as in interactive programs or debugging scenarios. | ||
- Use `\n` in most cases where immediate flushing isn't necessary for better performance. | ||
|
||
Understanding the differences between `std::endl` and `\n` helps in choosing the appropriate method based on the requirements of your program. | ||
|
||
Feel free to use either method based on your specific needs! |