🟢 Beginner  ·  Lesson 09

Input & Output – printf, scanf

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!

printf() से Output

C में printf() screen पर output display करने का standard function है। यह <stdio.h> में define है।

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

C Language
#include <stdio.h>
int main() {
    printf("Namaste Duniya!\n");
    printf("Meri age hai %d\n", 20);
    printf("Price: %.2f\n", 99.99f);
    printf("Grade: %c\n", 'A');
    return 0;
}

Format Specifiers

SpecifierData Typeउपयोग
%dintInteger print/input करने के लिए
%ffloatFloat number के लिए
%.2ffloat2 decimal places के साथ float
%lfdoubleDouble precision के लिए
%ccharSingle character के लिए
%sstringString के लिए
%%% symbol print करने के लिए

Escape Sequences

Sequenceअर्थEffect
\nNewlineअगली line पर जाता है
\tTabHorizontal tab (8 spaces) insert करता है
\\Backslash\ print करता है
\"Double quote" print करता है
\0NullString terminator

scanf() से Input

scanf() user से input पढ़ता है। Variable names से पहले & (address-of operator) लगाना ज़रूरी है।

C Language
#include <stdio.h>
int main() {
    int age;
    printf("Apni age enter karein: ");
    scanf("%d", &age);  // & ज़रूरी है!
    printf("Aapki age %d hai.\n", age);
    return 0;
}
⚠️ & मत भूलें!

scanf में & भूलने से program crash हो सकता है या गलत output आ सकता है। हमेशा scanf("%d", &variable) लिखें। Exception: char array/string में & की ज़रूरत नहीं।

Multiple Inputs

C Language
int a, b;
scanf("%d %d", &a, &b);   // दो integers space से separated
printf("Sum = %d\n", a + b);

Complete Program

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

    printf("Jod       = %.2f\n", a + b);
    printf("Ghatav    = %.2f\n", a - b);
    printf("Guna      = %.2f\n", a * b);
    if(b != 0) printf("Bhaag     = %.2f\n", a / b);
    return 0;
}

सारांश

  • printf() output display करता है — format specifiers ज़रूरी हैं
  • scanf() user input पढ़ता है — variable से पहले & लगाएं
  • Format specifiers: %d (int), %f (float), %c (char), %s (string)
  • Escape sequences: \n (newline), \t (tab)
  • scanf में & कभी न भूलें!
← Back to C Tutorial