← Back to C Course | Chapter 2: Input & Output | Lesson 2 of 7

C Output में Newline

\n escape sequence output को एक नई line पर ले जाता है, चाहे इसे एक single printf call के अंदर एक बार इस्तेमाल करें या कई अलग calls में दोहराएं।
Syntax
c
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

c
// 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;
}

एक printf में कई Lines

एक single printf call एक line तक सीमित नहीं है -- same string literal के अंदर कई \n sequences आ सकते हैं, हर एक string print होते समय एक नई line शुरू करता है।

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

c
// 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;
}

कई printf Calls में Newlines

Newlines को एक single printf call से आने की ज़रूरत नहीं है; कई consecutive calls साथ मिलकर multi-line output बना सकती हैं, इनमें से किसी में भी हर \n एक नई line शुरू करते हुए।

उदाहरण: Newlines Across Multiple printf Calls

c
// 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;
}

Newline बनाम No Newline

एक explicit \n के बिना, consecutive printf calls अपना text same line पर एक-दूसरे के तुरंत बाद print करते हैं, जो automatic line breaks की उम्मीद कर रहे beginners के लिए confusing output का एक common source है।

उदाहरण: Newline vs No Newline

c
// 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;
}

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

c
// 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;
}
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. output के अंत में \n भूल जाना, ताकि shell prompt same line पर दिखे।
  2. \n की बजाय /n लिखना, जो उन characters को literally print करता है।
  3. यह उम्मीद करना कि output तुरंत दिखेगा जब stdout buffered है और कोई newline print नहीं हुई।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.