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

scanf reads formatted values into variables (never forget the &), while getchar/getc read one buffered character, getch grabs a key instantly, and gets reads a line but is famously unsafe.

11 min read · 10 cards · 2 checks

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


Theory

The program gets ears

So far the marks program is a monologue: Riya's 78 is hard-coded, and any other student means editing source code.

Today it finally ASKS:

Enter marks: _

That blinking cursor waiting for a human is input, and C offers a small family of input statements. One of them (scanf) carries the most infamous single-character bug in beginner C, so today you also learn to never make it.

Theory

Delivery needs an address

Telling a courier "deliver to Riya" is useless; he needs the address of Riya's house. scanf is that courier: it collects a value from the keyboard and must deliver it INTO a variable's memory box. marks is the box's name, but &marks is its address, and couriers deliver to addresses. That is the whole mystery of the & sign: "at the address of".

Theory

scanf, formally

scanf("%d", &marks);

  • The format string "%d" announces what is coming (an int, here).
  • &marks hands over the address where the value must be stored.

Multiple values in one call work too: scanf("%d %d", &m1, &m2); reads two ints separated by spaces or Enter.

Forget the & and the value is delivered to a garbage address: crash or corrupted data, with no compiler error to warn you. It is the classic C beginner bug.

At a glance

The character-input cousins

FunctionReadsQuirk
getchar()One character, bufferedWaits until you press Enter
getc(stdin)One character, bufferedSame idea, file-flavoured form
getch()One keypress, instantNo Enter, no echo; conio.h, non-standard
gets()A whole line into a char arrayNo length check: UNSAFE, avoid

Practical

The marks program finally asks

#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

A student writes scanf("%d", marks); without the &. The program compiles. What happens when it runs?

  1. The value goes to a garbage address: crash or corrupted data
  2. It works fine, & is only a style convention
  3. The compiler quietly adds the missing &
  4. scanf politely asks the user to re-enter
Show the answer

The value goes to a garbage address: crash or corrupted data

Without &, scanf receives marks's current CONTENTS (garbage for an uninitialised int) and treats that number as a delivery address. Writing there is undefined behaviour: crashes on a good day, silent corruption on a bad one. Most compilers accept it with at most a warning, which is exactly why this bug is famous.

Think first

getch vs getchar: the press-any-key test

You want a "Press any key to continue..." screen that responds THE INSTANT a key is hit. Which function, and why not the other?

Show the answer

getch(): it grabs the keypress immediately, no Enter needed, no character echoed to the screen, which is exactly the press-any-key feel. getchar() is buffered: the program sees nothing until Enter arrives, so the screen would sit there ignoring your keystrokes. Remember getch's asterisk though: it lives in conio.h, Turbo C territory, non-standard.

Watch out

Where marks (and programs) die

The missing & in scanf: no compile error, runtime disaster; check every scanf twice. gets(): it never checks how much the user types, so 100 characters into a 20-char array overwrite neighbouring memory (buffer overflow, a real security hole). Exams expect: "gets is unsafe; prefer fgets". And note the header split: scanf/getchar/gets from stdio.h, getch from conio.h.

Theory

The program is now interactive

With scanf in hand, the marks program serves ANY student without recompiling, which is the entire point of software. Next lesson polishes the other direction: printf and its cousins, because a marksheet should not merely print, it should LOOK like a marksheet. The lesson after decodes the % symbols you have been using on trust.

Summary

Key takeaways

  • scanf("%d", &marks) reads formatted input; the & passes the variable's ADDRESS.
  • Missing & compiles fine and corrupts memory at runtime, the classic C bug.
  • getchar()/getc(stdin): one buffered character, delivered after Enter.
  • getch(): instant keypress, no echo; conio.h, non-standard (press-any-key screens).
  • gets() reads a line with no length check: unsafe, buffer overflow, avoid.
  • Memory hook: couriers deliver to addresses, & is the address.

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