Input and Output in C (printf() and scanf())
Input and Output (I/O) are essential operations in C programming. The C Standard Library provides functions to read data from the user and display output on the screen.
The header file required for these functions is:
#include <stdio.h>
1. printf() Function (Output)
The printf() function is used to display text, numbers, and variable values on the screen.
Syntax
printf("format string", variable1, variable2, ...);
Example
#include <stdio.h>
int main()
{
int age = 20;
printf("Age = %d\n", age);
return 0;
}
Output
Age = 20
Common Format Specifiers
| Format Specifier | Data Type | Example |
|---|---|---|
%d | Integer (int) | 25 |
%f | Float (float) | 12.50 |
%lf | Double (double) | 3.141592 |
%c | Character (char) | A |
%s | String | Hello |
%u | Unsigned Integer | 100 |
%x | Hexadecimal | 1A |
%o | Octal | 17 |
2. scanf() Function (Input)
The scanf() function is used to accept input from the user.
Syntax
scanf("format string", &variable);
Note: Use the address operator (
&) with variables inscanf(), except when reading a string into a character array.
Example
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("Your age is %d", age);
return 0;
}
Sample Output
Enter your age: 21
Your age is 21
Input Different Data Types
#include <stdio.h>
int main()
{
int age;
float salary;
char grade;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter salary: ");
scanf("%f", &salary);
printf("Enter grade: ");
scanf(" %c", &grade);
printf("\nAge = %d", age);
printf("\nSalary = %.2f", salary);
printf("\nGrade = %c", grade);
return 0;
}
Sample Output
Enter age: 22
Enter salary: 35000.75
Enter grade: A
Age = 22
Salary = 35000.75
Grade = A
Note: The space before
%cinscanf(" %c", &grade);skips any leftover whitespace or newline characters.
Reading a String
#include <stdio.h>
int main()
{
char name[30];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s", name);
return 0;
}
Sample Output
Enter your name: Rahul
Hello, Rahul
Limitation:
scanf("%s", name)reads only one word. To read a full line containing spaces, usefgets().
Example Program
#include <stdio.h>
int main()
{
int roll;
char name[30];
float marks;
printf("Enter Roll Number: ");
scanf("%d", &roll);
printf("Enter Name: ");
scanf("%s", name);
printf("Enter Marks: ");
scanf("%f", &marks);
printf("\n----- Student Details -----\n");
printf("Roll Number : %d\n", roll);
printf("Name : %s\n", name);
printf("Marks : %.2f\n", marks);
return 0;
}
Sample Output
Enter Roll Number: 101
Enter Name: Amit
Enter Marks: 88.50
----- Student Details -----
Roll Number : 101
Name : Amit
Marks : 88.50
Difference Between printf() and scanf()
printf() | scanf() |
|---|---|
| Displays output on the screen | Reads input from the user |
| Uses format specifiers to format output | Uses format specifiers to interpret input |
Does not require & with variables | Requires & for most variables (except strings) |
Summary
printf()is used to display output.scanf()is used to accept input from the keyboard.- Include the
<stdio.h>header file to use both functions. - Common format specifiers include
%d,%f,%lf,%c, and%s. - Use the address operator (
&) withscanf()for numeric and character variables. - Use
fgets()instead ofscanf("%s")when you need to read a full line of text containing spaces.
Category: MCA
