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

C
#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:

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

Output:

Code
Hello World! I am learning C

Printing on multiple lines

Use the newline character inside the string:

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

Output:

Code
Hello World!
I am learning C.

The golden rules

  • Put text inside double quotes &quot;...&quot;.
  • End each statement with a semicolon.
  • Without `

`, everything stays on the same line.

TL;DR

  • printf(&quot;text&quot;) prints text.
  • Two printf lines without `

` share one console line.

  • `

` inside a string jumps to a new line.

  • Keep all print calls in order; output appears top-to-bottom.