This is the first unit of C programming. Through the simplest program Experience the cycle of C program skeleton → compilation → execution.
Learning Objectives
- Understand the basic structure of C programs (
#include,main,printf,return). - You can compile source files with
gccand run them in the terminal. - Escape sequences (
\n,\t) and comments can be used.
Why start with “Hello World”?
Even if it is your first time seeing a program, if you succeed in displaying one line on the screen, It is verified at once that the environment (compiler, PATH, Korean encoding) is normal. That's why almost all introductory language books start with this example. Rather than “learning grammar,” the goal is to run through the development cycle.
Key concepts
1) Skeleton of C program
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Let's break down each line.
| element | Meaning |
|---|---|
#include <stdio.h> |
Preprocessing directive to include the standard input/output library (printf) header |
int main(void) |
The entry point function that the operating system calls first. The return type is int |
printf("...\n") |
Print string to standard output (stdout) |
return 0; |
Return exit code 0, indicating to the OS a "normal shutdown" |
2) 4 steps of compilation that occur at once
Although the process of turning a single source code file into an executable file is invisible, it consists of four steps.
hello.c hello (executable file)
│ ▲
│ ① Preprocessing ② Compile ③ Assemble ④ Link │
└─► cpp ─► cc1 ─► as ─► ld ──────┘
(#include expansion) (Create assembly) (Object) (Combining libraries)
gcc calls these four steps at once.
3) Escape sequence
These are characters that perform special actions within a string.
| notation | Meaning |
|---|---|
\n |
line break (newline) |
\t |
tab |
\\ |
backslash itself |
\" |
double quotes |
\0 |
null character (end of string) |
4) Comments
//One-line comments (since C99)
/*multiple lines
tin*/
Comments are ignored by the compiler and have no effect on execution.
See as example
Example 1 — ex01_hello.c: Simplest output
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
Execution result
Hello, World!
Bottom line: If \n is not present, the following prompt will be stuck on the same line: Try deleting \n yourself.
Example 2 — ex02_multiline.c: Multi-line output and tabs
printf("first line\n");
printf("Second line\n");
printf("Separated by tab\t\n");
printf("Print\nmultiple lines\nat once\n");
Execution result
first line
second line
separated by tabs
at once
multiple lines
Print
Key point: Even with printf once, if there are multiple \n, multiple lines will appear.
Example 3 — ex03_comments.c: Comments are not executed
//one line comment
/*printf("This line is not printed.");*/
printf("Comments have no effect on compilation.\n");
Execution result
Comments have no effect on compilation.
Bottom line: Temporarily disabling code is often wrapped in comments.
Looking at it from a different perspective — Cafe ordering analogy
A C program is similar to a cafe order form.
| Cafe | C code |
|---|---|
| menu board | <stdio.h> header (list of available functions) |
| Start Order | int main(void) { |
| Order Items | printf(...); |
| Payment completed | return 0; |
If there are no orders (=main is empty), the cafe will simply close and end.
Common Mistakes
- Missing semicolon: Compilation error if
;is not present at the end ofprintf("hi"). #includemissing: When usingprintf,<stdio.h>is missing → “implicit declaration” warning.Mainuppercase: C is case sensitive. The entry point must bemain.- Quotes in
printf("hi"): Double quotation marks (") and single quotation marks (') are different ('h'is one character). - Hangul is broken: If the terminal is not UTF-8, it will be broken. Linux/macOS Usually OK.
Cleanup
- The C source consists of 4 parts:
#include,main, main text, andreturn. - Compiled with
gcc -o namefile.c, executed with./name. printfdoes not automatically wrap lines, so you must use\ndirectly.- Comments have no effect on compilation, so use them freely.
- The first goal is to refine the environment so that one cycle (creation → build → execution) runs smoothly.
Try it yourself
cd src
gcc -std=c11 -Wall -o ex01 ex01_hello.c && ./ex01
gcc -std=c11 -Wall -o ex02 ex02_multiline.c && ./ex02
gcc -std=c11 -Wall -o ex03 ex03_comments.c && ./ex03
Application:
- Delete
\nfromex01_hello.cand see how the output changes. - Call
printfonly once and print 3 lines. (Hint:Line-1\nLine-2\nLine-3\n)