C++ में New Lines
In this page:
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
#include <iostream>
int main() {
std::cout << "Line one" << std::endl; // moves to new line and flushes
std::cout << "Line two" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
\n Escape Sequence
C से inherited \n escape sequence एक newline character represent करता है और सीधे एक string literal के अंदर embed किया जा सकता है ताकि output उस exact point पर एक नई line पर जाए।
उदाहरण: The \n Escape Sequence
// 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;
}
Login to try C/C++/Java/PHP code in the editor
endl बनाम \n
endl के उलट, \n बिना buffer flush force किए सिर्फ newline character insert करता है, जो एक tight loop में कई lines print करते समय इसे तेज़ choice बनाता है।
उदाहरण: endl vs \n
#include <iostream>
int main() {
for (int i = 0; i < 3; i++) {
std::cout << i << "\n"; // faster: no forced flush each iteration
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Statement में कई Lines
Output की कई lines या तो एक cout statement में कई \n-terminated strings chain करके, या हर एक endl पर खत्म होने वाले अलग cout statements इस्तेमाल करके बनाई जा सकती हैं।
उदाहरण: Multiple Lines in One Statement
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Blank Lines
text के दो pieces के बीच एक blank line दो consecutive newlines से produce होती है, चाहे दो chained endl manipulators की तरह लिखा गया हो या एक string में साथ रखे दो \n characters।
उदाहरण: Blank Lines
// 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;
}
Login to try C/C++/Java/PHP code in the editor
endlया\nभूल जाना, ताकि successive outputs एक line पर साथ चलें।\nकी बजाय/nलिखना, जो एक नई line की बजाय एक slash और एक n print करता है।- loops में हर जगह
endlइस्तेमाल करना, जो buffer भी flush करता है और\nके मुकाबले output धीमा कर सकता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: