← Back to C++ Course | Chapter 2: Input & Output | Lesson 3 of 8

C++ में New Lines

endl output को एक नई line पर ले जाता है और buffer flush करता है, जबकि \n escape sequence सिर्फ newline character insert करता है, tight loops के अंदर \n को तेज़ choice बनाते हुए।
Syntax
cpp
std::cout << "text" << std::endl;

std::cout << "text\n";

endl Manipulator

endl एक C++ stream manipulator है जो output को एक नई line पर ले जाता है और output buffer भी flush करता है, यह guarantee करते हुए कि अब तक print हुआ text actually तुरंत लिखा जाए।

उदाहरण: The endl Manipulator

cpp
#include <iostream>

int main() {
	std::cout << "Line one" << std::endl; // moves to new line and flushes
	std::cout << "Line two" << std::endl;
	return 0;
}

\n Escape Sequence

C से inherited \n escape sequence एक newline character represent करता है और सीधे एक string literal के अंदर embed किया जा सकता है ताकि output उस exact point पर एक नई line पर जाए।

उदाहरण: The \n Escape Sequence

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Line one\nLine two\n";
	// Return 0 to signal that the program finished successfully
	return 0;
}

endl बनाम \n

endl के उलट, \n बिना buffer flush force किए सिर्फ newline character insert करता है, जो एक tight loop में कई lines print करते समय इसे तेज़ choice बनाता है।

उदाहरण: endl vs \n

cpp
#include <iostream>

int main() {
	for (int i = 0; i < 3; i++) {
		std::cout << i << "\n"; // faster: no forced flush each iteration
	}
	return 0;
}

एक Statement में कई Lines

Output की कई lines या तो एक cout statement में कई \n-terminated strings chain करके, या हर एक endl पर खत्म होने वाले अलग cout statements इस्तेमाल करके बनाई जा सकती हैं।

उदाहरण: Multiple Lines in One Statement

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Line one\n" << "Line two\n";
	// Print to the console with cout
	std::cout << "Line three" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Blank Lines

text के दो pieces के बीच एक blank line दो consecutive newlines से produce होती है, चाहे दो chained endl manipulators की तरह लिखा गया हो या एक string में साथ रखे दो \n characters।

उदाहरण: Blank Lines

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Above" << std::endl << std::endl;
	// Print to the console with cout
	std::cout << "Below" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. endl या \n भूल जाना, ताकि successive outputs एक line पर साथ चलें।
  2. \n की बजाय /n लिखना, जो एक नई line की बजाय एक slash और एक n print करता है।
  3. loops में हर जगह endl इस्तेमाल करना, जो buffer भी flush करता है और \n के मुकाबले output धीमा कर सकता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.