C Output
1. Using printf() for Output
The most common way to display output in C is with the printf() function from the stdio.h library.
Basic Syntax:
-
"format string"→ The text you want to display. -
values→ Variables or constants to display. -
Placeholders in the format string are replaced by variable values.
2. Format Specifiers
Format specifiers tell printf() what type of data to print:
| Specifier | Type | Example |
|---|---|---|
%d |
Integer | int x = 10 |
%f |
Float | float pi = 3.14 |
%c |
Character | char ch = ‘A’ |
%s |
String | char str[] = “Hi” |
%lf |
Double | double d = 3.1415 |
%u |
Unsigned int | unsigned int u = 20 |
3. Examples of Output
Example 1: Display text only
Output:
Example 2: Display variables
Output:
Example 3: Multiple variables
Output:
4. Escape Sequences
Special characters that give formatting instructions:
| Escape Sequence | Meaning |
|---|---|
\n |
New line |
\t |
Horizontal tab |
\\ |
Backslash |
\" |
Double quote |
\' |
Single quote |
Example:
Output:
5. Tips for Using printf()
-
Always match format specifiers with variable types.
-
Use
%.nfto control decimal places for floats/doubles. -
Combine text and variables in a single statement for cleaner code.
