Skip to main content

printf and scanf in C

3 min read Updated May 29, 2025
Share:
On this page (14sections)

About this page

This is a focused tutorial in the C Data Input And Output group — format specifiers, field width, and scanf rules. The C Controls overview covers the same topic at chapter level; this page goes deeper with examples.

Introduction

printf and scanf are the most widely used formatted I/O functions in C, declared in <stdio.h>. printf writes formatted output to the screen; scanf reads formatted input from the keyboard.

printf — Formatted Output

#include <stdio.h>

int main(void) {
    int age = 25;
    float salary = 45000.50f;
    char name[] = "Kumar";

    printf("Name:   %s\n", name);
    printf("Age:    %d\n", age);
    printf("Salary: %.2f\n", salary);
    return 0;
}

Sample Output

Name:   Kumar
Age:    25
Salary: 45000.50

Common Format Specifiers

SpecifierTypeExample
%d or %iintprintf("%d", n);
%uunsigned intprintf("%u", n);
%ffloat / doubleprintf("%f", x);
%ccharprintf("%c", ch);
%sstring (char*)printf("%s", str);
%ppointer addressprintf("%p", ptr);
%%literal %printf("100%%");

Field Width and Precision

printf("%5d\n", 42);      /* "   42" — right-aligned in 5 chars */
printf("%-5d|\n", 42);     /* "42   |" — left-aligned */
printf("%.2f\n", 3.14159); /* "3.14" — 2 decimal places */
printf("%05d\n", 7);       /* "00007" — zero-padded */

scanf — Formatted Input

#include <stdio.h>

int main(void) {
    int age;
    float height;
    char name[50];

    printf("Enter name: ");
    scanf("%49s", name);

    printf("Enter age and height: ");
    scanf("%d %f", &age, &height);

    printf("Hello %s, age %d, height %.1f\n", name, age, height);
    return 0;
}

Sample Run

Enter name: Priya
Enter age and height: 22 5.4
Hello Priya, age 22, height 5.4

Important Rules for scanf

  • Always pass addresses for numeric and char variables: &age, &height.
  • For strings (char[]), pass the array name directly (it is already an address).
  • Limit string length to prevent buffer overflow: scanf("%49s", name) for a 50-char array.
  • scanf returns the number of successfully read items — check the return value in production code.

printf Special Characters

EscapeMeaning
\nNew line
\tTab
\\Backslash
\"Double quote

See Escape Sequences in C for the full list.

Best Practices

  • Prefer fgets over scanf("%s") for reading full lines with spaces.
  • Never use %s without a width limit on fixed-size buffers.
  • Flush output before waiting for input if mixing printf and scanf in some environments.
  • Match every format specifier to the correct argument type.

Common Mistakes

  • Forgetting & in scanf (scanf("%d", n) instead of scanf("%d", &n)).
  • Using %f in scanf for double — use %lf for double in scanf (but %f in printf works for both).
  • Reading a char after a number without consuming the leftover newline.

Practice with Printf And Scanf Example Program.

Frequently Asked Questions

Why must I use & before variables in scanf?
scanf needs the memory address of the variable to store input. The & operator gives the address of a variable.
What happens if format specifiers do not match the arguments?
It causes undefined behaviour — always match %d with int, %f with float/double, %s with char array, etc.

Related Tutorials

Search tutorials