A program that only works with fixed, hardcoded values is rarely useful in the real world. Most software needs to communicate with the people using it, whether that means displaying results on a screen or accepting information typed in by a user. This two-way communication is handled through input and output operations, and in C, this is primarily done using two functions that you will use constantly throughout your programming journey.
Although printf and scanf look simple on the surface, they hide a surprising amount of detail once you start working with different data types, formatting requirements, and edge cases involving user input. Taking the time to understand these functions properly now will save you from confusing bugs later, especially once your programs start handling more complex combinations of input.
In this tutorial, you will learn how to display output using printf, how to accept user input using scanf, what format specifiers and escape sequences are, how to work with individual characters using getchar and putchar, and some of the most common mistakes beginners run into when working with input and output in C.
The printf function is used to display text and values on the screen. It is one of the very first functions every C programmer learns, and it remains one of the most frequently used throughout even advanced programs.
#include <stdio.h>
int main() {
printf("Welcome to CS Engineering Gyan!");
return 0;
}
Welcome to CS Engineering Gyan!
When printf is given plain text without any format specifiers, it simply displays that text exactly as written. Things become more interesting once variables are introduced into the output using format specifiers.
Format specifiers are special placeholders inside a printf or scanf statement that tell the compiler what type of data is being displayed or read. Each data type in C has its own corresponding specifier.
| Specifier | Used For |
|---|---|
| %d | Displaying or reading an integer value. |
| %f | Displaying or reading a floating-point value. |
| %c | Displaying or reading a single character. |
| %s | Displaying or reading a string of characters. |
| %lf | Reading a double value using scanf, since %f alone is used for double values only in printf. |
#include <stdio.h>
int main() {
int episode = 25;
float rating = 4.7;
char grade = 'A';
printf("Episode %d rated %.1f, grade %c", episode, rating, grade);
return 0;
}
Episode 25 rated 4.7, grade A
Notice how each format specifier lines up with a corresponding variable listed after the format string, in the same order they appear.
The scanf function allows a program to read values typed in by the user during execution, making programs interactive rather than limited to fixed, predetermined data.
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You entered: %d", age);
return 0;
}
Enter your age: 21 You entered: 21
Notice the ampersand symbol placed before the variable name inside scanf. This symbol represents the address-of operator, and it tells scanf exactly where in memory the entered value should be stored.
scanf is not limited to reading a single value at a time. Multiple format specifiers can be combined within a single scanf call to accept several pieces of input together.
#include <stdio.h>
int main() {
int day, month, year;
printf("Enter date as day month year: ");
scanf("%d %d %d", &day, &month, &year);
printf("Date entered: %d/%d/%d", day, month, year);
return 0;
}
Enter date as day month year: 15 8 2026 Date entered: 15/8/2026
When multiple values are separated by spaces on the same line, scanf automatically matches each typed value to the corresponding format specifier and variable in order.
Escape sequences are special character combinations that represent characters which cannot be typed directly into a string, such as a new line or a tab space. They always begin with a backslash.
| Escape Sequence | Meaning |
|---|---|
| \n | Moves the cursor to the beginning of the next line. |
| \t | Inserts a horizontal tab space. |
| \\ | Displays a single backslash character. |
| \" | Displays a double quote character within a string. |
#include <stdio.h>
int main() {
printf("CS Engineering Gyan\nSubscribe for more tutorials!");
return 0;
}
CS Engineering Gyan Subscribe for more tutorials!
Besides printf and scanf, C also provides dedicated functions for working with single characters, which can be useful for simple, character-based input and output tasks.
| Function | Purpose |
|---|---|
| getchar | Reads a single character typed by the user. |
| putchar | Displays a single character on the screen. |
#include <stdio.h>
int main() {
char letter;
printf("Enter a grade letter: ");
letter = getchar();
printf("You entered: ");
putchar(letter);
return 0;
}
Enter a grade letter: A You entered: A
Reading a full word or sentence requires a slightly different approach compared to reading numbers or single characters, since strings in C are stored as arrays of characters.
#include <stdio.h>
int main() {
char channelName[30];
printf("Enter a channel name: ");
scanf("%s", channelName);
printf("Channel entered: %s", channelName);
return 0;
}
Enter a channel name: CSEngineeringGyan Channel entered: CSEngineeringGyan
Unlike variables of other types, arrays do not require an ampersand before their name in scanf, since the array name itself already represents the memory address where the data will be stored. It is also worth noting that %s stops reading at the first space, which means it cannot capture multi-word input directly.
printf allows fine control over how numbers are displayed, including how many decimal places to show or how much space a value should occupy, which is especially useful when formatting output into neat, aligned columns.
#include <stdio.h>
int main() {
float averageWatchTime = 6.856;
printf("Average watch time: %.2f minutes", averageWatchTime);
return 0;
}
Average watch time: 6.86 minutes
The number placed between the percent sign and the letter f controls how many digits appear after the decimal point, rounding the value automatically if necessary.
| Mistake | Correct Practice |
|---|---|
| Forgetting the ampersand before a variable name in scanf. | Always include the address-of operator when reading into a simple variable using scanf. |
| Using %f to read a double value with scanf. | Use %lf specifically when reading double values with scanf, even though %f is used for both in printf. |
| Expecting %s to capture input containing spaces. | Understand that %s stops at the first space, and consider alternative approaches when full sentences are needed. |
| Mismatching the number of format specifiers with the number of variables provided. | Ensure the number and order of format specifiers exactly matches the variables listed in printf or scanf. |
Input and output operations are what allow a C program to interact meaningfully with the people using it, transforming static code into something genuinely interactive. By mastering printf for displaying formatted output and scanf for accepting user input, along with supporting tools like escape sequences and character-based functions, you gain the ability to build programs that respond dynamically to real user data.
In this tutorial, you learned how to display values using printf, accept single and multiple inputs using scanf, work with escape sequences and format specifiers, handle individual characters, and control the precision of floating-point output. With these fundamentals in place, you are ready to move on to conditional statements, which allow your programs to make decisions based on the values they read or calculate.