gcc --versionWriting Your First C Program: A Simple Step-by-Step Example
What is Writing Your First C Program? Core Concept & Direct Summary
How do you write, compile, and run a classic 'Hello, World!' program in C from the command line?
There's a moment every programmer remembers: the first time a machine obeys their command. You type a few lines of strange-looking text, press a key, and — there it is. Your words, on the screen, becauseyoutold the computer to put them there.
That moment is about ten minutes away.
In this tutorial, you'll write, compile, and run your very first C program. No prior experience needed. I'll explain every single line, show you the exact commands to type, and — just as valuable — show you the three mistakes almost every beginner makes, so you can dodge them in advance.
Let's build something.
How Does Writing Your First C Program Work? In-Depth Mechanics & Architecture
What You'll Need (2 Minutes of Setup)
A C program needs only two things: atext editorand acompiler.
- Text editor— anything works: Notepad, TextEdit, VS Code, even your phone. The editor just holds your code.
- Compiler— the translator that converts your code into a runnable program. The most popular isGCC(GNU Compiler Collection).
Quick install check:open your terminal (or Command Prompt) and type:
See a version number? You're ready. See an error? You'll need to install GCC first — on macOS it comes with Xcode Command Line Tools (xcode-select --install), on Windows the easiest route is MinGW, and on Linux it's usuallysudo apt install gcc. (Ourwalks through each platform in detail.)
Step 1: Create the File
Open your text editor and create a new file named:
That.cextension matters — it's how the compiler recognizes a C source file. Save it somewhere easy to find, like your Desktop or a newcodingfolder.
Step 2: Type the Program
Here's the complete program. Type it exactly as shown — yes, every bracket and semicolon:
Six lines. That's a complete, working C program. Don't worry about understanding it yet — we'll dissect it in a moment. Right now, the goal is to see itrun.
Typing tip:resist the urge to copy-paste for this first one. Typing it yourself, slowly, is how your fingers start learning C.
Step 3: Compile It
Open your terminal, navigate to where you saved the file, and run:
Let's decode that command:
gcc— calls the compilerhello.c— the file to translate-o hello— "name the output programhello"
If the terminal stays silent and returns to the prompt —congratulations, that means it worked.Compilers only speak up when something's wrong. You now have a brand-new executable file sitting next to your source code.
Step 4: Run It
On macOS/Linux:
On Windows:
And then, the moment:
The computer just followed your instructions. You're officially a C programmer. 🎉
Step 5: Understand Every Line
Now the satisfying part — let's dissect what you just built:
This is apreprocessor directive. Before compiling, C literally copies the contents ofstdio.h(thestandardinput/output header) into your program. That file contains the recipe forprintf()— without this line, the compiler wouldn't know whatprintfmeans.
This is themain function— the mandatory starting point of every C program. When your program launches, execution begins here. The{opens the function's body: everything between{and}belongs tomain.
The heart of the program.printf(printformatted) sends text to the screen. Two details:
- Thedouble quotesmark the text as astring— literal characters, not code.
- The
\nis anescape sequencemeaning "new line" — it moves the cursor to the next line after printing. Try deleting it and recompiling: the output runs into your prompt. Small detail, instant visible difference.
And notice thesemicolonat the end — every statement in C ends with one.
This hands control back to the operating system with a status code.0means "success."Any other number signals that something went wrong. The closing}ends themainfunction — and the program.
Level Up: Make ItYourProgram (Step 6)
Hello World is tradition, but let's make it personal. Replace the middle line with this:
Output:
New concepts, painlessly introduced:
char name[]— astring variablestoring textint year— aninteger variablestoring a whole number%sand%d—format specifiers: placeholders thatprintffills with your variables' values
Change the name, change the year, recompile, rerun. That little loop —edit, compile, run— is exactly how every C program on Earth gets built, from student exercises to the Linux kernel.
The 3 Mistakes Every Beginner Makes (So You Won't)
❌ Mistake 1: The Missing Semicolon
Symptom:compiler error likeexpected ';' before 'return'.Fix:every statement ends with;. Check the lineabovethe error message.
❌ Mistake 2: Forgetting #include
Symptom:implicit declaration of function 'printf'.Fix:#include <stdio.h>at the very top.
❌ Mistake 3: Unmatched Braces or Quotes
Symptom:errors about unterminated strings or unexpected end of file.Fix:quotes and braces always come in pairs:"...",{...}. Count them when in doubt.
Here's the secret experienced programmers know:compiler errors aren't failures — they're the compiler teaching you.Every C programmer alive has seen thousands of them.
What to Try Next (Mini Challenges)
Don't close your editor yet. Momentum matters. Try these five-minute challenges:
- Three lines— print your name, city, and favorite food on separate lines (hint: three
printfcalls, or\nbetween them) - Break it on purpose— delete a semicolon, recompile, read the error, fix it. Error messages become far less scary once you've caused a few yourself
- Do some math— add
printf("5 + 3 = %d\n", 5 + 3);and see C calculate for you - Ask a question— look ahead with
scanf("%d", &year);toreadinput instead of only printing
Each tiny win builds the muscle memory that makes everything after easier.
Runnable C Code Example & Practical Implementation
#include <stdio.h>
int main() {
// Print greeting to the screen
printf("Hello, World!\
");
return 0;
}Key Takeaways, Best Practices & Memory Safety Rules
int main(void) or int main(int argc, char *argv[]) and return 0 for POSIX and ANSI compliance.Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked C programming interview questionsQ: What happens if you omit `#include `?
Q: Is `void main()` standard in C?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online C Compiler & Runner — Interactive online GCC sandbox to execute C code in real time.
- Online C Code Editor & IDE — Multi-file C programming environment with code formatting.
- C++ Programming Tutorial — Transition from procedural C to Object-Oriented C++ with STL.
- JavaScript Programming Tutorial — Master modern JavaScript fundamentals, DOM APIs, and async patterns.
Authoritative Standards & Citations
- ISO/IEC 9899 (C Language Standard) — Official international C specification by ISO/IEC JTC 1/SC 22/WG 14.
- GNU GCC Documentation — Official reference manual for the GNU Compiler Collection and C libraries.
- The C Programming Language (K&R) — Definitive classic work by Brian Kernighan & Dennis Ritchie at Bell Labs.
- C Reference Documentation (cppreference.com) — Verified reference covering C89 through C23 language features.