C++ के Escape Sequences
In this page:
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
// 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
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक Windows path
"C:\new\test"की तरह लिखना, जहाँ\nऔर\tएक newline और एक tab बन जाते हैं; हर backslash को\\की तरह double करना ज़रूरी है। - एक string के अंदर एक double quote को
\"की तरह escape किए बिना डालना, जो string को जल्दी खत्म कर देता है और compile तोड़ देता है। - एक नई line के लिए
\nकी बजाय/nलिखना, जो सिर्फ एक slash के बाद एक letter n print करता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: