Input statements: scanf(), getc(), getch(), gets(), getchar()

scanf formatted values variables में पढ़ता है (& कभी मत भूलिए), जबकि getchar/getc एक buffered character पढ़ते हैं, getch एक key तुरंत पकड़ता है, और gets एक line पढ़ता है पर मशहूर रूप से असुरक्षित है।

11 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Program को कान मिलते हैं

अब तक marks program एक monologue है: Riya के 78 hard-coded हैं, और कोई भी दूसरा student मतलब source code editing।

आज यह आख़िरकार पूछता है:

Enter marks: _

किसी इंसान का इंतज़ार करता वह टिमटिमाता cursor ही input है, और C input statements का एक छोटा परिवार देता है। इनमें से एक (scanf) beginner C का सबसे बदनाम एक-character वाला bug ढोता है, इसलिए आज आप उसे कभी न करना भी सीखते हैं।

Theory

Delivery को पता चाहिए

Courier से "Riya को deliver करो" कहना बेकार है; उसे Riya के घर का पता चाहिए। scanf वही courier है: keyboard से एक value इकट्ठा करता है और उसे किसी variable के memory डिब्बे के अंदर deliver करना होता है। marks डिब्बे का नाम है, पर &marks उसका पता है, और courier पतों पर deliver करते हैं। & चिह्न का पूरा रहस्य यही है: "के पते पर"।

Theory

scanf, ठीक-ठीक

scanf("%d", &marks);

  • format string "%d" बताती है कि क्या आ रहा है (यहाँ एक int)।
  • &marks वह पता थमाता है जहाँ value store होनी है।

एक call में कई values भी चलती हैं: scanf("%d %d", &m1, &m2); spaces या Enter से अलग 2 ints पढ़ता है।

& भूल गए तो value एक garbage पते पर deliver होती है: crash या गड़बड़ data, और आपको चेताने वाला कोई compiler error नहीं। यही classic C beginner bug है।

At a glance

Character-input वाले भाई-बंधु

Functionक्या पढ़ता हैख़ासियत
getchar()एक character, bufferedEnter दबाने तक इंतज़ार
getc(stdin)एक character, bufferedवही idea, file-flavoured रूप
getch()एक keypress, तुरंतन Enter, न echo; conio.h, non-standard
gets()पूरी line एक char array मेंकोई length जाँच नहीं: UNSAFE, टालिए

Practical

Marks program आख़िरकार पूछता है

#include <stdio.h>

int main() {
    int marks;
    printf("Enter marks: ");
    scanf("%d", &marks);

    printf("You entered: %d\n", marks);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Quiz

एक student & के बिना scanf("%d", marks); लिखता है। Program compile हो जाता है। चलने पर क्या होता है?

  1. Value garbage पते पर जाती है: crash या गड़बड़ data
  2. ठीक चलता है, & सिर्फ़ एक style का रिवाज है
  3. Compiler चुपचाप छूटा हुआ & जोड़ देता है
  4. scanf विनम्रता से user से दोबारा डालने को कहता है
Show the answer

Value garbage पते पर जाती है: crash या गड़बड़ data

& के बिना scanf को marks का मौजूदा CONTENT मिलता है (uninitialised int के लिए garbage) और वह उस number को delivery का पता मान लेता है। वहाँ लिखना undefined behaviour है: अच्छे दिन crash, बुरे दिन चुपचाप corruption। ज़्यादातर compilers इसे बहुत हुआ तो एक warning के साथ मान लेते हैं, इसीलिए यह bug मशहूर है।

Think first

getch बनाम getchar: press-any-key test

आप ऐसा "Press any key to continue..." screen चाहते हैं जो key दबते ही तुरंत जवाब दे। कौन सा function, और दूसरा क्यों नहीं?

Show the answer

getch(): यह keypress तुरंत पकड़ता है, Enter की ज़रूरत नहीं, screen पर कोई character echo नहीं, बिल्कुल वही press-any-key वाला एहसास। getchar() buffered है: Enter आने तक program को कुछ नहीं दिखता, इसलिए screen आपकी keystrokes को नज़रअंदाज़ करती बैठी रहती। पर getch का asterisk याद रखिए: यह conio.h में रहता है, Turbo C का इलाक़ा, non-standard।

Watch out

Marks (और programs) कहाँ मरते हैं

scanf में छूटा हुआ &: कोई compile error नहीं, runtime में तबाही; हर scanf 2 बार जाँचिए। gets(): यह कभी नहीं देखता कि user कितना type करता है, इसलिए 20-char array में 100 characters पड़ोसी memory पर लिख देते हैं (buffer overflow, असली security छेद)। Exams चाहते हैं: "gets असुरक्षित है; fgets बेहतर"। और header का बँटवारा याद रखिए: scanf/getchar/gets stdio.h से, getch conio.h से।

Theory

Program अब interactive है

scanf हाथ में आते ही marks program बिना recompile किए किसी भी student को serve करता है, और software का पूरा मक़सद यही है। अगला lesson दूसरी दिशा चमकाता है: printf और उसके भाई, क्योंकि marksheet को सिर्फ़ print नहीं होना चाहिए, उसे marksheet जैसा दिखना चाहिए। उसके बाद वाला lesson उन % चिह्नों को decode करता है जिन्हें आप भरोसे पर इस्तेमाल करते आए हैं।

Summary

Key takeaways

  • scanf("%d", &marks) formatted input पढ़ता है; & variable का ADDRESS पास करता है।
  • छूटा हुआ & ठीक compile होता है और runtime पर memory corrupt करता है, classic C bug।
  • getchar()/getc(stdin): एक buffered character, Enter के बाद मिलता है।
  • getch(): तुरंत keypress, कोई echo नहीं; conio.h, non-standard (press-any-key screens)।
  • gets() बिना length जाँच के line पढ़ता है: असुरक्षित, buffer overflow, टालिए।
  • याद रखने का hook: courier पतों पर deliver करते हैं, & ही पता है।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to C language

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Input statements: scanf(), getc(), getch(), gets(), getchar() · Computer Programming and Programming Methodology (CPPM) · Gri-Learn