Aral Code

Getting Started Lesson 3 of 16

C Output with printf and Format Specifiers

Print text, integers, decimals and characters with printf, use format specifiers like %d, %f, %.2f, %c and %s, set field widths, and write comments.

printf ("print formatted") is C's main way to show output. It prints a template string, replacing special format specifiers with values you provide.

Printing text

#include <stdio.h>

int main(void) {
    printf("Line one\n");
    printf("Line two\n");
    printf("Same ");
    printf("line\n");
    return 0;
}

Output

Line one
Line two
Same line

Unlike Java's println, printf never adds a new line by itself. You must include \n.

Format specifiers

#include <stdio.h>

int main(void) {
    int age = 16;
    double average = 92.4567;
    char section = 'A';

    printf("Age: %d\n", age);
    printf("Average: %f\n", average);
    printf("Average: %.2f\n", average);
    printf("Section: %c\n", section);
    printf("School: %s\n", "Rizal NHS");
    printf("%d + %d = %d\n", 7, 8, 7 + 8);
    return 0;
}

Output

Age: 16
Average: 92.456700
Average: 92.46
Section: A
School: Rizal NHS
7 + 8 = 15
SpecifierType
%d or %iint
%ldlong
%fdouble (and float); 6 decimals by default
%.2fdouble with 2 decimals
%cchar
%sstring (text)
%%a real % sign

Field width and alignment

#include <stdio.h>

int main(void) {
    printf("|%5d|\n", 42);       /* right-aligned in 5 spaces */
    printf("|%-5d|\n", 42);      /* left-aligned */
    printf("|%05d|\n", 42);      /* padded with zeros */
    printf("|%8.2f|\n", 3.14159);
    printf("|%-10s|%6s|\n", "Kape", "8.00");
    printf("Discount: 20%%\n");
    return 0;
}

Output

|   42|
|42   |
|00042|
|    3.14|
|Kape      |  8.00|
Discount: 20%

A neat receipt

#include <stdio.h>

int main(void) {
    printf("%-12s %4s %9s\n", "ITEM", "QTY", "AMOUNT");
    printf("%-12s %4d %9.2f\n", "Pandesal", 10, 25.00);
    printf("%-12s %4d %9.2f\n", "Kape 3-in-1", 4, 32.00);
    printf("%-12s %4d %9.2f\n", "Itlog", 6, 54.00);
    printf("------------------------------\n");
    printf("%-17s %12.2f\n", "TOTAL", 111.00);
    return 0;
}

Output

ITEM          QTY    AMOUNT
Pandesal       10     25.00
Kape 3-in-1     4     32.00
Itlog           6     54.00
------------------------------
TOTAL                   111.00

Escape sequences

SequenceMeaning
\nNew line
\tTab
\"Double quote
\\Backslash

puts

puts("text") prints a string and automatically adds a new line. It's simpler than printf when there's nothing to format.

Comments

#include <stdio.h>

/* A multi-line comment:
   Program: Receipt Printer
   Author:  Juan Dela Cruz */
int main(void) {
    // A single-line comment (C99 and later)
    puts("Salamat po!");
    return 0;
}

Output

Salamat po!

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which specifier prints an int?
2. What does printf("%.1f", 3.456) print?
3. How do you print a real % sign?
4. Does printf add a new line automatically?

By Sir Reyel · Last updated 2026-12-04