C Output में Newline
In this page:
printf("first line\nsecond line\n");
\n Escape Sequence
\n escape sequence एक newline character represent करता है, और इसे एक string के अंदर रखना printf को output में exactly उसी point पर cursor को अगली line की शुरुआत में ले जाने को कहता है।
उदाहरण: The \n Escape Sequence
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Line one\nLine two");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक printf में कई Lines
एक single printf call एक line तक सीमित नहीं है -- same string literal के अंदर कई \n sequences आ सकते हैं, हर एक string print होते समय एक नई line शुरू करता है।
उदाहरण: Multiple Lines in One printf
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Line one\nLine two\nLine three");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई printf Calls में Newlines
Newlines को एक single printf call से आने की ज़रूरत नहीं है; कई consecutive calls साथ मिलकर multi-line output बना सकती हैं, इनमें से किसी में भी हर \n एक नई line शुरू करते हुए।
उदाहरण: Newlines Across Multiple printf Calls
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Line one\n");
// Print formatted text to the screen
printf("Line two\n");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Newline बनाम No Newline
एक explicit \n के बिना, consecutive printf calls अपना text same line पर एक-दूसरे के तुरंत बाद print करते हैं, जो automatic line breaks की उम्मीद कर रहे beginners के लिए confusing output का एक common source है।
उदाहरण: Newline vs No Newline
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Hello");
// Print formatted text to the screen
printf("World");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Line Endings में Platform Differences
C standard library text-mode stream में लिखते समय \n और operating system के actual line-ending convention के बीच translation automatically handle करती है, इसलिए same \n Linux, macOS, और Windows पर सही से काम करता है।
उदाहरण: Platform Differences in Line Endings
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("This works the same on Linux, macOS, and Windows\n");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- output के अंत में
\nभूल जाना, ताकि shell prompt same line पर दिखे। \nकी बजाय/nलिखना, जो उन characters को literally print करता है।- यह उम्मीद करना कि output तुरंत दिखेगा जब stdout buffered है और कोई newline print नहीं हुई।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: