C के puts() और gets()
In this page:
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()?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
puts() बनाम printf() इस्तेमाल करना
plain text output के लिए puts() printf से इस्तेमाल करना आसान है क्योंकि यह कोई format specifiers नहीं लेता, लेकिन यह printf के %d, %s, और similar placeholders की तरह किसी string के बीच में variable values interpolate भी नहीं कर सकता।
उदाहरण: Using puts() vs. printf()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Gets और Buffer Sizes को समझना
fgets() के उलट, पुराना gets() function कभी check नहीं करता कि destination buffer type किए जा रहे input के लिए काफी बड़ा है या नहीं, जो exactly वह security flaw है जो इसे किसी भी real program में इस्तेमाल करने के लिए unsafe बनाता है।
उदाहरण: Understanding Gets and Buffer Sizes
#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;
}
Login to try C/C++/Java/PHP code in the editor
Input से Newlines साफ करना
fgets() जो buffer पढ़ता है उसमें trailing newline character शामिल होता है, gets() के उलट जो इसे discard कर देता है -- अगर आपका code उस string को बिना newline वाली किसी चीज़ से compare करता है, तो आपको पहले इसे strip करना होगा, अक्सर strcspn या manual indexing से।
उदाहरण: Cleaning Newlines from Input
// 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;
}
Login to try C/C++/Java/PHP code in the editor
gets()इस्तेमाल करना, जिसकी कोई size limit नहीं और यह buffer overflow कर सकता है; इसे C11 में हटा दिया गया था।- यह मान लेना कि
putsexactlyprintfजैसा behave करता है, जब यह हमेशा एक newline add करता है। fgetsइस्तेमाल करना और यह भूल जाना कि यह buffer में trailing newline रखता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: