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()
- Forgetting
&for variables - Not using space before
%cto ignore leftover newline - Using
%sfor full name without space handling
Practice Questions
- What is the difference between printf() and scanf()?
- Why do we use & in scanf()?
- Write a program to input and print name, age, and marks.
- Write a program to input two numbers and print the largest.
- 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.