Skip to study material
← Question index

QUESTION 50 · Input and output

Explain printf and scanf with suitable format specifiers.

5 marks

Answer in brief

Formatted input and output functions read and display values using a format string. They are declared in the stdio.h header.

printf: formatted output

printf displays values on standard output, usually the screen. A format specifier tells it how to display each value. For example, printf("Age = %d", age); displays an integer. For price = 12.5, printf("%.2f", price); displays 12.50. Here .2 requests two digits after the decimal point. It changes the displayed form, not the stored value.

scanf: formatted input

scanf reads values from standard input, usually the keyboard. For int age;, use scanf("%d", &age);. The & operator gives the address where the value must be stored. Check that scanf returns 1 before using this one value. A different result means the required assignment did not succeed. The input must also be within the range of the destination type.

Common format rules

  1. Integer: Use %d for int.
  2. Floating input: Use %f with float * and %lf with double *. For example, scanf("%lf", &price); reads a double.
  3. Floating output: Use %f for a double. A float passed to printf is converted to double automatically.
  4. Character: Use %c for a character.
  5. Long double: Use %Lf for both input and output with the appropriate argument.

Important points

The format specifier and argument type must match. scanf normally needs addresses; printf normally needs the values to display. stdin is standard input, stdout is normal output and stderr is error output. These streams can be redirected to files. A successful input count does not prove that the whole line is valid.

Reference tables

↔ Scroll horizontally on a small screen
Common format specifiers
Value typeprintfscanf
int%d%d with int *
unsigned int%u%u with unsigned int *
long long%lld%lld with long long *
float%f (promoted to double)%f with float *
double%f%lf with double *
long double%Lf%Lf with long double *
char%c%c with char *
size_t%zu%zu with size_t *
↔ Scroll horizontally on a small screen
Standard streams
StreamRoleExample
stdinStandard input, often connected to the keyboard.scanf reads from stdin.
stdoutNormal program output.printf writes to stdout.
stderrDiagnostic/error output.fprintf(stderr, "Invalid input\n");

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
int main(void)
{
    double price;
    if (scanf("%lf", &price) != 1) {
        puts("Invalid input");
        return 1;
    }
    printf("Price = %.2f\n", price);
    return 0;
}

Sample input and output

Input

12.5

Output

Price = 12.50

Key points

  • Match every format to the actual argument type.
  • Check input success before reading the result.

Related exam questions

Topic matches from the supplied JNTUH papers. Marks below are the printed allocations for those questions.

  • March/April 2023 · Q3(b) · 5 marks

    Distinguish formatted and unformatted input/output statements.

    Paper P07 · page 1
  • September 2023 · Q2(a) · 3 marks

    Explain stdin, stdout and stderr with examples.

    Paper P09 · page 1
View all matching exam references →