🟢 Beginner  ·  Lesson 09

Input & Output – printf, scanf

Output with printf()

In C, printf() is the standard function to display output on the screen. It is defined in <stdio.h>.

Syntax: printf("format string", variables);

C Language
#include <stdio.h>
int main() {
    printf("Hello, World!\n");              // Simple string
    printf("My age is %d\n", 20);           // Integer
    printf("Price: %.2f\n", 99.99f);         // Float, 2 decimals
    printf("Grade: %c\n", 'A');              // Character
    printf("%d + %d = %d\n", 5, 3, 5+3);    // Multiple values
    return 0;
}
Hello, World! My age is 20 Price: 99.99 Grade: A 5 + 3 = 8

Format Specifiers

Format specifiers tell printf/scanf what type of data to expect or print.

SpecifierData TypeExampleOutput
%dintprintf("%d", 42)42
%iint (same as %d)printf("%i", 42)42
%ffloatprintf("%f", 3.14f)3.140000
%.2ffloat, 2 decimalsprintf("%.2f", 3.14f)3.14
%lfdoubleprintf("%lf", 3.14)3.140000
%ccharprintf("%c", 'A')A
%sstringprintf("%s", "Hello")Hello
%ldlong intprintf("%ld", 100L)100
%lldlong long intprintf("%lld", 99999LL)99999
%uunsigned intprintf("%u", 200)200
%oOctalprintf("%o", 8)10
%xHexadecimalprintf("%x", 255)ff
%pPointer addressprintf("%p", ptr)0x7fff...
%%Literal %printf("100%%")100%

Width and Precision Formatting

C Language
printf("%5d\n", 42);     // "   42"  — right-aligned, width 5
printf("%-5d|\n", 42);    // "42   |" — left-aligned, width 5
printf("%05d\n", 42);    // "00042" — zero-padded, width 5
printf("%.4f\n", 3.14159f); // "3.1416" — 4 decimal places
printf("%8.2f\n", 3.14f); // "    3.14" — width 8, 2 decimals

Escape Sequences

Special characters that can't be typed directly.

Escape Seq.MeaningEffect
\nNewlineMoves to next line
\tTabInserts horizontal tab (8 spaces)
\\BackslashPrints a \
\"Double quotePrints a "
\'Single quotePrints a '
\rCarriage returnMoves to beginning of line
\0Null characterString terminator
\aBellAudio beep (system)
\bBackspaceMoves cursor back one position

Input with scanf()

scanf() reads input from the user. The & (address-of) operator is required before variable names (except strings).

Syntax: scanf("format", &variable);

C Language
#include <stdio.h>
int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);  // & is required — gives address of 'age'
    printf("You are %d years old.\n", age);
    return 0;
}
Enter your age: 20 You are 20 years old.
⚠️ Don't forget &

Forgetting & in scanf causes undefined behavior (program crash or wrong output). Always write scanf("%d", &variable) not scanf("%d", variable). Exception: char arrays/strings don't need &.

Reading Multiple Inputs

C Language
#include <stdio.h>
int main() {
    int a, b;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);   // Two integers, space-separated
    printf("Sum = %d\n", a + b);

    float price;
    char  grade;
    scanf("%f %c", &price, &grade);

    return 0;
}

Complete Programs

Program: Simple Calculator

C Language – Calculator
#include <stdio.h>
int main() {
    float a, b;
    printf("Enter two numbers: ");
    scanf("%f %f", &a, &b);

    printf("Sum        = %.2f\n", a + b);
    printf("Difference = %.2f\n", a - b);
    printf("Product    = %.2f\n", a * b);
    if (b != 0)
        printf("Division   = %.2f\n", a / b);
    else
        printf("Division by zero!\n");
    return 0;
}
Enter two numbers: 10 4 Sum = 14.00 Difference = 6.00 Product = 40.00 Division = 2.50

getchar() and putchar()

getchar() reads a single character. putchar() prints a single character.

C Language
#include <stdio.h>
int main() {
    char ch;
    printf("Enter a character: ");
    ch = getchar();
    printf("You entered: ");
    putchar(ch);
    putchar('\n');
    return 0;
}

Summary

  • printf() is used to display output — requires format specifiers
  • scanf() reads user input — requires & before variable names
  • Format specifiers: %d (int), %f (float), %lf (double), %c (char), %s (string)
  • Escape sequences: \n (newline), \t (tab), \\ (backslash)
  • Width/precision: %5d, %.2f, %8.3lf
  • Never forget & in scanf — causes undefined behavior!

Frequently Asked Questions

How do printf and scanf work in C?
printf displays text and values on the screen, using format specifiers like %d and %f to show numbers. scanf reads input typed by the user into variables, using the same kind of specifiers and the address of each variable with &.
What are format specifiers in C?
Format specifiers are placeholders in printf and scanf that tell C the type of data being printed or read. Common ones are %d for integers, %f for floats, %c for characters and %s for strings.
Why do you use & in scanf but not in printf?
scanf needs the memory address of a variable so it can store the input there, which is why you write &x. printf only needs the value to display, so it takes the variable directly without &.
What are escape sequences in C?
Escape sequences are special character combinations starting with a backslash that represent things you cannot type directly, such as \n for a new line and \t for a tab. They are used inside strings to control formatting.
What is the difference between getchar and scanf?
getchar reads a single character from input, while scanf can read formatted data of various types such as numbers and strings. getchar is simpler and useful for reading one character at a time, often in a loop.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.