Printing literal strings
What you'll learn: How to display exact sequences of text on your screen using the printf function.
The Messenger of C
In programming, your code often needs to "speak" to the person using the computer. In the C language, the most common way to do this is through a function called printf. Think of printf as a specialized messenger. You give it a message, and it delivers that message directly to the console window.
When you want the messenger to deliver a message exactly as you wrote it—letter for letter—we call that a literal string.
Wrapping Your Message
Computer languages are very picky. If you simply type a sentence into your code, C will try to read it as a set of commands and get confused. To tell C, "This is just text, don't try to run it," you must wrap your words in double quotation marks (" ").
Imagine you are sending a physical letter. The double quotes are the envelope. The computer doesn't care what is inside the envelope; it just knows that everything between the " and the " is the cargo it needs to display.
How it Looks in Code
To use printf, you place your quoted string inside the parentheses. Every command in C also ends with a semicolon (;), which acts like a period at the end of a sentence.
#include <stdio.h>
int main() {
// This tells C to print the exact text inside the quotes
printf("Hello, world!");
printf("C is fun.");
return 0;
}
In the example above, the computer sees printf("Hello, world!"); and immediately pushes the characters Hello, world! to your screen. Note that the quotation marks themselves don't show up in the final result—they are just the "packaging" used to define where the text starts and ends.
The "Literal" Part
We call this a "literal" string because the computer is not doing any math or logic with it. It is literally just copying the symbols you typed. If you type printf("5 + 5");, the screen won't show 10; it will show the literal characters 5 + 5.
Key takeaway
To display text, pass a "literal string" inside double quotes to the printf() function.