</>
Skip to content
C lessons (5/28)

C — Output

printf — formatted output

printf is C's primary output function. It prints formatted text to the console:

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}

The \n at the end adds a newline. Without it, the cursor stays at the end of the line.

Format specifiers

#include <stdio.h>

int main() {
    int age = 36;
    double pi = 3.14159;
    char grade = 'A';
    char name[] = "Ada";

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Pi: %.2f\n", pi);
    printf("Grade: %c\n", grade);
    return 0;
}
SpecifierTypeExample
%dint42
%ldlong int9000000000L
%ffloat/double3.14
%.2ffloat with 2 decimals3.14
%cchar'A'
%sstring"hello"
%ppointer0x7fff...
%%literal %%
%zusize_t8

Width and alignment

printf("%10d\n", 42);        // "        42" — right-aligned in 10 chars
printf("%-10d|\n", 42);      // "42        |" — left-aligned
printf("%05d\n", 42);        // "00042" — zero-padded
printf("%10.2f\n", 3.14);    // "      3.14" — right-aligned float
printf("%-10.2f|\n", 3.14);  // "3.14      |" — left-aligned float
printf("%10s|\n", "hello");  // "     hello|" — right-aligned string

Multiple values

int x = 10;
int y = 20;
printf("x = %d, y = %d, sum = %d\n", x, y, x + y);

printf accepts any number of arguments — one format specifier for each.

Escape sequences

printf("New line\n");        // \n — newline
printf("Tab\there\n");      // \t — tab
printf("Backslash: \\\n");   // \\ — literal backslash
printf("Quote: \"hello\"\n");// \" — literal quote
printf("Null: \0\n");        // \0 — null character
printf("Bell: \a\n");        // \a — bell sound

Writing to files

#include <stdio.h>

int main() {
    FILE *f = fopen("output.txt", "w");
    if (f == NULL) {
        printf("Error opening file\n");
        return 1;
    }

    fprintf(f, "Name: Ada\n");
    fprintf(f, "Age: %d\n", 36);
    fclose(f);

    printf("Written to output.txt\n");
    return 0;
}

fprintf works like printf but writes to a file instead of the console.

Reading input with scanf

#include <stdio.h>

int main() {
    int age;
    printf("How old are you? ");
    scanf("%d", &age);
    printf("You are %d years old.\n", age);
    return 0;
}

scanf reads formatted input. The & before the variable passes its address — required for scanf to modify it.

Reading strings

#include <stdio.h>

int main() {
    char name[50];
    printf("Enter your name: ");
    scanf("%49s", name);  // limit to 49 characters
    printf("Hello, %s!\n", name);
    return 0;
}

%s stops reading at whitespace. For full lines, use fgets:

char line[100];
printf("Enter a sentence: ");
fgets(line, sizeof(line), stdin);
printf("You wrote: %s", line);

Reading multiple values

int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a + b);

String formatting with sprintf

#include <stdio.h>

int main() {
    char buffer[100];
    int age = 36;
    sprintf(buffer, "Name: Ada, Age: %d", age);
    printf("%s\n", buffer);
    return 0;
}

sprintf writes formatted text to a string buffer instead of the console.

puts vs printf

puts("Hello");      // prints "Hello\n" — adds newline automatically
printf("Hello\n");  // same result — more control

puts is simpler but less flexible. Use printf when you need format specifiers.

getchar and putchar

#include <stdio.h>

int main() {
    printf("Press any key: ");
    int c = getchar();  // read one character
    printf("You pressed: ");
    putchar(c);         // print one character
    putchar('\n');
    return 0;
}

getchar and putchar handle single characters — useful for character-by-character processing.

Common mistakes

// Forgetting & in scanf
int x;
scanf("%d", x);    // WRONG — crash or undefined behavior
scanf("%d", &x);   // correct

// Buffer overflow
char name[10];
scanf("%s", name);  // DANGEROUS — no length limit
scanf("%9s", name); // safer — max 9 characters

// Forgetting newline after scanf
int x;
scanf("%d", &x);
char c;
scanf("%c", &c);  // reads the leftover newline!

Mini Practice

  1. Print a formatted receipt with item names, quantities, and prices
  2. Read a user's name and age, then print a formatted introduction
  3. Use fprintf to write data to a file
  4. Use sprintf to build a formatted string
  5. Read two numbers from the user and print their sum, difference, and product

Next: commenting your C code →

Related Topics

Frequently Asked Questions about Output

What is Output in C?

Output is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Output?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Output.

Why is Output important in C?

Output is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.