OnlineCBT Logo OnlineCBT
C Tutorial
C · LESSON 5

Input and Output: printf and scanf

By Ratan Updated: 15 Oct 2026

Read user input using scanf() address-of operator (&) and print formatted output.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Read input and report a result

Console input is text. A C program must convert that text into the type it needs, check whether conversion succeeded, and only then use the value. This chapter uses scanf for a small numeric exercise and introduces safer line-based input for later programs.

Match formats to arguments

printf uses %d for int, %c for a character, %s for a null-terminated string and %f for a floating-point argument. float is promoted to double in this call. With scanf, use %d with int*, %f with float* and %lf with double*. A format/type mismatch can cause undefined behavior.

Worked program: rectangle area

#include 
int main(void) {
    double width = 0.0, height = 0.0;
    printf("Enter width and height: ");
    if (scanf("%lf %lf", &width, &height) != 2) {
        fputs("Expected two numbers.\n", stderr);
        return 1;
    }
    if (width <= 0.0 || height <= 0.0) {
        fputs("Dimensions must be positive.\n", stderr);
        return 1;
    }
    printf("Area = %.2f\n", width * height);
    return 0;
}

Input 3 4 gives Area = 12.00. Input three 4 takes the error path. The ampersand supplies an address where scanf can store the converted number. Its return value is the number of successful assignments, not the number the user typed.

Whitespace and bounded strings

Most scanf conversions skip leading whitespace. %c does not; " %c" requests whitespace skipping first. Do not append a newline to an interactive numeric scanf format: that can make it keep waiting for a later non-whitespace character.

An unbounded %s may overflow a buffer. For char name[20], %19s leaves room for the null terminator, but still reads only one whitespace-delimited word. To accept a full name, read a whole line with fgets and validate or parse it separately. Never use gets.

When scanf is not enough

Checking its return value does not solve every input problem. Out-of-range numeric input needs care, and failed input can remain in the stream. Robust interactive applications generally use fgets followed by strtol or strtod with end-pointer and range checks. Do not use fflush(stdin) as a portable way to clear input.

Practice

  1. Read quantity and unit price; reject zero or negative quantity and negative price.
  2. For quantity 4 and price 12.5, expect a total of 50.00.
  3. Test letters instead of a number. Explain why the multiplication must not run after failed conversion.
  4. Explain the difference between &price in scanf and price in printf: the first supplies storage, the second supplies a value.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat