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

C का scanf()

scanf वह पढ़ता है जो कोई व्यक्ति type करता है और इसे एक variable में store करता है, आपका answer लिखने वाले एक teacher की तरह। आप इसे बताते हैं किस type की उम्मीद है और कहाँ रखना है।
Syntax
c
scanf("%specifier", &variable_name);

scanf() क्या है?

scanf ("scan formatted") keyboard पर user के type किए input को पढ़ता है और इसे आपके program के variables में store करता है, इसे उस standard तरीके की तरह बनाते हुए जिससे एक C program सिर्फ hardcoded data से चलने की बजाय interactive बनता है।

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

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

एक Integer पढ़ना

एक int पढ़ने के लिए %d specifier और variable नाम से पहले एक ampersand चाहिए (scanf("%d", &age);), क्योंकि scanf को value लिखने के लिए variable का memory address चाहिए होता है -- variable के address की बजाय खुद variable pass करना एक classic beginner bug है।

उदाहरण: Reading an Integer

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

एक Character पढ़ना

%c से एक single character पढ़ना पिछली scanf call से input buffer में बची whitespace या newline characters के प्रति sensitive है, जो अगली read को चुपचाप आपकी उम्मीद वाले character की बजाय एक space पकड़ने का कारण बन सकती है।

उदाहरण: Reading a Character

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

एक Floating-Point Value पढ़ना

%f एक float variable में एक decimal number पढ़ता है, आपके program को सिर्फ source code में baked values की बजाय user input से सीधे measurements या prices जैसी चीज़ें accept करने देते हुए।

उदाहरण: Reading a Floating-Point Value

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

एक साथ कई Values पढ़ना

एक single scanf call एक साथ कई values पढ़ सकता है, जैसे scanf("%d %f", &count, &price);, जब तक format string हर specifier को variables और उनके बाद आने वाले matching addresses के same order में list करे।

उदाहरण: Reading Multiple Values Together

c
// 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;
}
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. scanf("%d", x) में & भूल जाना, जो address की बजाय value pass करता है और crash करता है।
  2. scanf में एक double के लिए %f इस्तेमाल करना, जब %lf ज़रूरी है।
  3. scanf का return value check न करना, ताकि bad input unnoticed रहे।
🔒

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.