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

C++ के Escape Sequences

Escape sequences text के अंदर special two-character codes हैं जो new line या tab जैसी चीज़ों के लिए खड़े होते हैं, secret shortcuts की तरह। Backslash C++ को बताता है 'अगला letter special है'।
Syntax
cpp
std::cout << "text\nnew line\ttab\\ backslash \"quote\"";

Newline Escape Sequence

\n सीधे एक string literal के अंदर एक newline insert करता है, आपको कई अलग cout statements की ज़रूरत के बिना output को कई lines में तोड़ने देते हुए।

यह C++ में सबसे commonly इस्तेमाल होने वाला escape sequence है, लगभग हर उस program में दिखता है जो एक single line से ज़्यादा print करता है।

उदाहरण: Newline 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;
}

Tab Escape Sequence

\t एक horizontal tab insert करता है, जो आमतौर पर output के columns line up करने के लिए इस्तेमाल होता है — उदाहरण के लिए, names की एक list को scores की एक list के against align करना ताकि वे एक table जैसे पढ़ें।

एक tab की exact width terminals के बीच अलग हो सकती है, इसलिए precise alignment के लिए <iomanip> से setw अक्सर ज़्यादा reliable choice है।

उदाहरण: Tab 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 << "Name\tScore\n";
	// Print to the console with cout
	std::cout << "Amit\t95\n";
	// Return 0 to signal that the program finished successfully
	return 0;
}

Quotes और Backslashes

", ', और \ जैसे characters C++ string और character literals में special meaning रखते हैं, इसलिए उन्हें literally print करने के लिए एक preceding backslash से escape करना ज़रूरी है — एक double-quoted string के अंदर एक quote के लिए \", या एक literal backslash के लिए \\।

escape के बिना, compiler character को string को जल्दी खत्म करने की तरह misinterpret करेगा।

उदाहरण: Quotes and Backslashes

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

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "She said \"hello\"" << std::endl;
	// Print to the console with cout
	std::cout << "Path: C:\\Users" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Carriage Return

\r, carriage return, cursor को एक नई line पर बढ़े बिना current line की शुरुआत में वापस ले जाता है, जो बाद वाले एक print को जो पहले से वहाँ है उसे overwrite करने देता है।

यही trick terminal programs में simple in-place progress indicators के पीछे है, जैसे एक percentage counter जो scroll करने की बजाय same line पर update होता है।

उदाहरण: Carriage Return

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

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Loading...\r";
	// Print to the console with cout
	std::cout << "Done!      " << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Alarm या Bell Sound

\a print होने पर terminal की alert sound या visual bell trigger करता है, एक literal physical bell वाले early terminal hardware से एक holdover।

यह modern applications में शायद ही कभी इस्तेमाल होता है, लेकिन इसे support करने वाले एक terminal में चलने वाले command-line tools में एक simple audible notification की तरह अभी भी काम कर सकता है।

उदाहरण: Alarm or Bell Sound

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

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Alert\a" << 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. एक Windows path "C:\new\test" की तरह लिखना, जहाँ \n और \t एक newline और एक tab बन जाते हैं; हर backslash को \\ की तरह double करना ज़रूरी है।
  2. एक string के अंदर एक double quote को \" की तरह escape किए बिना डालना, जो string को जल्दी खत्म कर देता है और compile तोड़ देता है।
  3. एक नई line के लिए \n की बजाय /n लिखना, जो सिर्फ एक slash के बाद एक letter n print करता है।
🔒

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.