Input / Output in C (printf & scanf)

C provides two main functions for input and output: printf() → used for output scanf() → used for input Both are defined inside the <stdio.h> header file.

What is printf()?

printf() is used to display output on the screen.

printf("Hello");
printf("Number: %d", 10);
printf("Value: %.2f", 3.14);

Format Specifiers Used With printf()

%d  → integer  
%f  → float  
%c  → character  
%s  → string  
%lf → double  
%u  → unsigned int  
%x  → hexadecimal  
%o  → octal  

What is scanf()?

scanf() is used to take input from the user. It stores the value inside variables.

Important: Variable address must be passed using & (address-of operator). Exception: arrays (strings) do NOT need &.

scanf("%d", &age);
scanf("%f", &price);
scanf("%c", &grade);
scanf("%s", name);     // no & for string

Basic Example of printf & scanf

#include <stdio.h>

int main() {

    int age;
    float marks;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your marks: ");
    scanf("%f", &marks);

    printf("\nAge: %d", age);
    printf("\nMarks: %.2f", marks);

    return 0;
}

Important: scanf() Stops at Space

scanf() cannot read strings with spaces.

scanf("%s", name);   // "Sourav Sahu" → only reads "Sourav"

To read full line with spaces, use:

scanf("%[^\n]s", name);   // reads full name including spaces

Multiple Input at Once

scanf("%d %f %c", &age, &marks, &grade);

Practical Example 2 – Taking String Input

#include <stdio.h>

int main() {

    char name[50];

    printf("Enter your full name: ");
    scanf("%[^\n]s", name);

    printf("Your Name: %s", name);

    return 0;
}

Practical Example 3 – Calculator Using Input

#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);
    printf("Division: %.2f\n", a / b);

    return 0;
}

Practical Example 4 – Reading Characters

#include <stdio.h>

int main() {

    char ch;

    printf("Enter a character: ");
    scanf(" %c", &ch);   // space before %c removes previous newline

    printf("You entered: %c", ch);

    return 0;
}

Practical Example 5 – Reading Multiple Data Types Together

#include <stdio.h>

int main() {

    int roll;
    float cgpa;
    char grade;

    printf("Enter Roll Number, CGPA and Grade: ");
    scanf("%d %f %c", &roll, &cgpa, &grade);

    printf("\n--- Student Details ---");
    printf("\nRoll: %d", roll);
    printf("\nCGPA: %.2f", cgpa);
    printf("\nGrade: %c", grade);

    return 0;
}

Practical Example 6 – Temperature Conversion Using User Input

#include <stdio.h>

int main() {

    float celsius;

    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);

    float fahrenheit = (celsius * 9/5) + 32;

    printf("Temperature in Fahrenheit: %.2f°F\n", fahrenheit);

    return 0;
}

Practical Example 7 – Reading Full Address (Multiple Words)

#include <stdio.h>

int main() {

    char address[100];

    printf("Enter your full address: ");
    scanf("%[^\n]s", address);   // reads entire line

    printf("\nYour Address: %s", address);

    return 0;
}

Common Mistakes with scanf()

Practice Questions

  1. What is the difference between printf() and scanf()?
  2. Why do we use & in scanf()?
  3. Write a program to input and print name, age, and marks.
  4. Write a program to input two numbers and print the largest.
  5. Explain why scanf("%c") needs a space before %c.

Practice Task

Create a program that takes: • Your full name • Age • Marks • Grade Then display them in a clean formatted output.