C का scanf()
In this page:
scanf("%specifier", &variable_name);
scanf() क्या है?
scanf ("scan formatted") keyboard पर user के type किए input को पढ़ता है और इसे आपके program के variables में store करता है, इसे उस standard तरीके की तरह बनाते हुए जिससे एक C program सिर्फ hardcoded data से चलने की बजाय interactive बनता है।
उदाहरण: What is scanf()?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int age;
sscanf("25", "%d", &age);
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Integer पढ़ना
एक int पढ़ने के लिए %d specifier और variable नाम से पहले एक ampersand चाहिए (scanf("%d", &age);), क्योंकि scanf को value लिखने के लिए variable का memory address चाहिए होता है -- variable के address की बजाय खुद variable pass करना एक classic beginner bug है।
उदाहरण: Reading an Integer
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int age;
sscanf("25", "%d", &age);
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Character पढ़ना
%c से एक single character पढ़ना पिछली scanf call से input buffer में बची whitespace या newline characters के प्रति sensitive है, जो अगली read को चुपचाप आपकी उम्मीद वाले character की बजाय एक space पकड़ने का कारण बन सकती है।
उदाहरण: Reading a Character
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char grade;
sscanf("A", "%c", &grade);
// Print formatted text to the screen
printf("%c", grade);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Floating-Point Value पढ़ना
%f एक float variable में एक decimal number पढ़ता है, आपके program को सिर्फ source code में baked values की बजाय user input से सीधे measurements या prices जैसी चीज़ें accept करने देते हुए।
उदाहरण: Reading a Floating-Point Value
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
float price;
sscanf("19.99", "%f", &price);
// Print formatted text to the screen
printf("%.2f", price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक साथ कई Values पढ़ना
एक single scanf call एक साथ कई values पढ़ सकता है, जैसे scanf("%d %f", &count, &price);, जब तक format string हर specifier को variables और उनके बाद आने वाले matching addresses के same order में list करे।
उदाहरण: Reading Multiple Values Together
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int count;
float price;
sscanf("3 9.99", "%d %f", &count, &price);
// Print formatted text to the screen
printf("%d %.2f", count, price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
scanf("%d", x)में&भूल जाना, जो address की बजाय value pass करता है और crash करता है।scanfमें एकdoubleके लिए%fइस्तेमाल करना, जब%lfज़रूरी है।scanfका return value check न करना, ताकि bad input unnoticed रहे।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: