Course contentsShow
← C
Lesson 42 of 1,2191. FoundationsFree lesson

Basic `printf` syntax

Basic printf syntax

What you'll learn: How to use the printf function to display plain text on your computer screen.

The Messenger of C

In C programming, your code needs a way to talk back to you. The most common tool for this is a function called printf, which is short for "print formatted." Think of printf as a highly obedient messenger. You provide a message inside specific "packaging," and the function ensures that message appears in your terminal window. Without it, your programs would run in total silence, performing calculations behind the scenes without ever showing you the results.

The Anatomy of the Command

To use printf, you must follow a strict set of grammar rules so the computer understands exactly where your message starts and ends. Here is the standard structure:

#include <stdio.h>

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

Let’s break down that specific line:

  1. The Name: printf tells the computer which tool to use.
  2. The Parentheses: The () act like a container. Everything inside these parentheses is what you are handing over to the function to be processed.
  3. The Double Quotes: The "" are perhaps the most important part. They act like bookends. C uses these to distinguish between "code" (instructions for the computer) and "strings" (raw text meant for humans). Anything inside the quotes is treated as literal text.
  4. The Semicolon: The ; at the end is like a period at the end of a sentence. It tells C that this specific instruction is finished.

Why Quotes Matter

Imagine you have a dog named "Walk." If you shout, "Walk!", the dog gets excited. If you show the dog a sign that says "Walk," it might just tilt its head. In C, if you type printf(Hello), the computer looks for a command or a variable named Hello. Because that doesn't exist, the program crashes. By using quotes—printf("Hello")—you are telling C: "Don't try to understand this word; just display it exactly as it is."

Key takeaway

Use printf("Your text here"); to send a message to the screen, always remembering to wrap your text in double quotes and end the line with a semicolon.