C Output and Format Specifiers
3 min read ·
Output in C is used to display information on the screen. The most commonly used function for output is
printf().The
printf() function is part of the standard input output library and is used to print text, variables, and formatted data.Explanation
printf() displays text on the screen
The text must be written inside double quotes
Note
To use printf(), you must include the stdio.h header file.
Printing Variables
You can also print variables using
printf() by using format specifiers.Explanation
%d is a format specifier for integers
It tells printf what type of value to expect
Common Format Specifiers
Format specifiers define the type of data being printed.
%d for integers
%f for float values
%lf for double values
%c for characters
%s for strings
Explanation
\n is used to move output to the next line
%.1f controls decimal places for float
%.5lf controls precision for double
Pro Tip
You can control how many decimal places are printed using precision like %.2f.
Multiple Values in One printf
You can print multiple variables in a single statement.
Explanation
Multiple format specifiers can be used in one printf
Values are printed in the same order as variables
Escape Sequences in C
Escape sequences are special characters used inside printf.
\n for new line
\t for tab space
" for double quote
Explanation
\n moves output to a new line
\t adds spacing
Real World Scenario
Escape sequences are used to format output properly, such as printing structured reports or tables.
Common Mistakes
Explanation
Using the wrong format specifier can produce incorrect output
Always match the specifier with the data type
Caution
Mismatch between format specifier and variable type can lead to undefined behavior.
Goal Achieved
You now understand how to use printf() and format specifiers to display output correctly in C.
Exercise
Write a C program that prints your name, age, and percentage using appropriate format specifiers.