Appearance
[中文版本](/posts/c教程/zh-CN/01-入门/003-Writing and Running Your First Program)
Now that your compiler is ready, it’s time to write your first real C program. This is where the magic happens, you’ll write plain text, compile it into machine instructions, and watch your computer follow your commands exactly.
C doesn’t hide what’s happening under the hood. Every step, writing, compiling, linking, running, is visible and under your control.
Tiny Code
Create a new file namedhello.c and type this code:
#include <stdio.h>
int main(void) {
printf("Hello, world!\n");
return 0;
}Then, compile and run it:
gcc hello.c -o hello
./helloYou should see:
Hello, world!Breaking It Down
#include <stdio.h> This tells the compiler to use the Standard Input/Output library, which provides theprintf function.
int main(void) Every C program starts with amain function. It’s the entry point, where execution begins.
printf("Hello, world!\n"); This prints text to the screen. The\n means “newline,” so the next output starts on a new line.
return 0; Whenmain returns 0, it tells the operating system that your program finished successfully.
Why It Matters
Your “Hello, world” may look simple, but it represents an entire process:
- The compiler translates your text (
hello.c) into object code (hello.o). - The linker combines that code with standard libraries.
- The executable (
hello) is pure machine instructions. - The operating system loads and runs it.
Understanding this flow is what makes C special, it’s not just about writing code, but about knowing how code becomes software.
Try It Yourself
Change the message to"Hello, C learner!" and recompile.
Add another line:
printf("This is my first C program.\n");Try leaving out the semicolon, what error does the compiler show?
Try removing#include <stdio.h>, what happens then?
Experiment and break things. Every error teaches you how C thinks.
You’ve just written and run your first C program, a direct conversation between you and the machine. From here, every new piece of code builds on this simple moment of control and understanding.
