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 એનું સરનામું છે, અને couriers સરનામે 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 બગાડે છે, 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, ટાળો.
  • યાદ રાખવાની યુક્તિ: couriers સરનામે 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