Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Hello, World
By the end of this lesson you can write a complete C program, compile it, and print exact text with printf, including text that changes with the input. C is the language underneath operating systems and most language runtimes, and it does almost nothing for you automatically. That starts with printing.
The smallest complete program
#include <stdio.h>
int main(void) {
printf("Tickets: sold out\n");
return 0;
}
Output:
Tickets: sold out
#include <stdio.h>is a preprocessor directive. Before compiling, the standard input/output header is pasted in, and that header declaresprintf. C has no automatic imports. You include every header you use.int main(void)is the entry point, the function the operating system runs first. It returns anintto the OS.return 0;reports success. Any non-zero value means failure, and shells and CI pipelines really do read it.- Every statement ends with
;. Leave one out and the compiler's error usually points at the next line.
C is compiled: gcc -Wall -o tickets tickets.c turns the source into a program, and ./tickets runs it. -Wall turns on warnings. Treat every warning as a bug report, because C compiles many programs that are wrong.
printf adds nothing on its own
printf writes exactly the characters you give it. It adds no newline and no space. A newline is the escape sequence \n, which is a backslash followed by n inside the quotes. Other escapes: \t is a tab, \" is a double quote, \\ is a backslash.
printf("Tea");
printf("Coffee\n"); /* prints: TeaCoffee */
printf("Tea\n");
printf("Coffee\n"); /* prints Tea, then Coffee on its own line */
Two printf calls do not make two lines. Only \n ends a line. /n with a forward slash is not a newline. It prints a slash and an n.
Placeholders: %s fills in text
The first argument to printf is the format string. A % inside it starts a conversion specifier, a placeholder that printf replaces with the next argument. %s means "put a piece of text here":
char city[] = "Lisbon"; /* a variable holding text */
printf("Next stop: %s.\n", city);
printf("Doors open on the %s.\n", "left");
Output:
Next stop: Lisbon.
Doors open on the left.
The characters around %s are printed exactly as typed, including the . and the space before %s. Change city to "Porto" and the same line prints Next stop: Porto. with no other edit. That is the point of a placeholder: the program works for text it has never seen. The Numbers and printf lesson adds %d and %f for numbers.
Since % is special, a literal percent sign in the format string is written %%. For example, printf("Battery: 80%%\n"); prints Battery: 80%.
Never use outside text as the format string
If text comes from the user, pass it as an argument to %s. Do not pass it as the format string itself:
printf(city); /* wrong: any % inside city is treated as a placeholder */
printf("%s", city); /* right: city is printed exactly as it is */
If city held 100% Lisbon, the wrong version would read % L as a placeholder and print garbage or crash. gcc -Wall warns about this for a reason.
Common mistakes
- Typing a value from the example instead of using the variable. The output is right for the example and wrong for every other input. Hidden tests use other inputs.
- Forgetting
\nbetween lines. The two lines run together, as inTeaCoffee. - Changing the punctuation or spacing.
Hello,Ada!orHello, Ada !is a different string. The comparison checks every character. - Passing the input as the format string. An input containing
%then prints the wrong text or crashes.
Your exercise
The starter already reads one line of text into name. Your job is the two printf lines that greet that name. Most first attempts fail because they type the example's name instead of using the %s placeholder.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…