Lesson 16 +10 XP

Printing Text with printf

Printing Text with printf

The workhorse of C output is printf, which lives in the <stdio.h> header.

One line

#include <stdio.h>

int main() {
    printf("Hello World!");
    return 0;
}

This prints: Hello World!

Two printf statements, same line

Both printf statements write with no break in between, so the console shows one line:

printf("Hello World!");
printf(" I am learning C");

Output:

Hello World! I am learning C

Printing on multiple lines

Use the newline character \n inside the string:

printf("Hello World!\n");
printf("I am learning C.\n");

Output:

Hello World!
I am learning C.

The golden rules

  • Put text inside double quotes "...".
  • End each statement with a semicolon.
  • Without \n, everything stays on the same line.

TL;DR

  • printf("text") prints text.
  • Two printf lines without \n share one console line.
  • \n inside a string jumps to a new line.
  • Keep all print calls in order; output appears top-to-bottom.