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

C के puts() और gets()

puts text की एक line print करता है और fgets safely एक line पढ़ता है, एक speaker और एक careful listener की तरह। gets पुराना dangerous तरीका है, जिससे बचना चाहिए।
Syntax
c
puts("text");  // adds a newline automatically

fgets(string_name, size, stdin);  // safe line input

puts() क्या है?

puts() एक string print करता है जिसके बाद automatically एक newline आती है, इसे printf का एक थोड़ा simpler alternative बनाते हुए जब आपको बिना किसी variable substitution या formatting के सिर्फ fixed text दिखाना हो।

उदाहरण: What is puts()?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	puts("Hello, World!");
	// Return 0 to signal that the program finished successfully
	return 0;
}

puts() बनाम printf() इस्तेमाल करना

plain text output के लिए puts() printf से इस्तेमाल करना आसान है क्योंकि यह कोई format specifiers नहीं लेता, लेकिन यह printf के %d, %s, और similar placeholders की तरह किसी string के बीच में variable values interpolate भी नहीं कर सकता।

उदाहरण: Using puts() vs. printf()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	puts("Fixed text, no formatting");
	// Print formatted text to the screen
	printf("Age: %d\n", 25);
	// Return 0 to signal that the program finished successfully
	return 0;
}

fgets() से Text सुरक्षित तरीके से पढ़ना

gets() deprecated है और modern C standards से हटाया जा चुका है क्योंकि इसके पास यह limit करने का कोई तरीका नहीं है कि यह कितना input पढ़ता है, user के input को buffer overflow करने और adjacent memory corrupt करने देते हुए -- fgets() इसे एक maximum length लेकर safely replace करता है।

उदाहरण: Reading Text safely with fgets()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	char buffer[20] = "Sample text\n";
	// Print formatted text to the screen
	printf("%s", buffer);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Gets और Buffer Sizes को समझना

fgets() के उलट, पुराना gets() function कभी check नहीं करता कि destination buffer type किए जा रहे input के लिए काफी बड़ा है या नहीं, जो exactly वह security flaw है जो इसे किसी भी real program में इस्तेमाल करने के लिए unsafe बनाता है।

उदाहरण: Understanding Gets and Buffer Sizes

c
#include <stdio.h>
int main() {
	char buffer[10];
	// gets(buffer) would not check if input fits in buffer -- unsafe
	printf("Use fgets(buffer, sizeof(buffer), stdin) instead");
	return 0;
}

Input से Newlines साफ करना

fgets() जो buffer पढ़ता है उसमें trailing newline character शामिल होता है, gets() के उलट जो इसे discard कर देता है -- अगर आपका code उस string को बिना newline वाली किसी चीज़ से compare करता है, तो आपको पहले इसे strip करना होगा, अक्सर strcspn या manual indexing से।

उदाहरण: Cleaning Newlines from Input

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
	char buffer[20] = "Hello\n";
	buffer[strcspn(buffer, "\n")] = '\0';
	// Print formatted text to the screen
	printf("[%s]", buffer);
	// 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. gets() इस्तेमाल करना, जिसकी कोई size limit नहीं और यह buffer overflow कर सकता है; इसे C11 में हटा दिया गया था।
  2. यह मान लेना कि puts exactly printf जैसा behave करता है, जब यह हमेशा एक newline add करता है।
  3. fgets इस्तेमाल करना और यह भूल जाना कि यह buffer में trailing newline रखता है।
🔒

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.