Topic 6 of 100 Fundamentals of C Interactive Tutorial

Writing Your First C Program: A Simple Step-by-Step Example

9 mins read Last updated: 22 Aug 2026 31,200 views
C Compiler 0.00%

What is Writing Your First C Program? Core Concept & Direct Summary

Core Objective & Problem Statement

How do you write, compile, and run a classic 'Hello, World!' program in C from the command line?

Direct Answer Summary

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:

main.c
Run Online
#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}

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 compiler
  • hello.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:

main.c
Run Online
#include <stdio.h>
int main() {
    char name[] = "Ravi";
    int year = 2026;
    printf("Hello! My name is %s.\n", name);
    printf("I wrote my first C program in %d.\n", year);
    return 0;
}

Output:

main.c
Run Online
Hello! My name is Ravi.
I wrote my first C program in 2026.

New concepts, painlessly introduced:

  • char name[]— astring variablestoring text
  • int 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

main.c
Run Online
printf("Hello!\n") // ← no semicolon!

Symptom:compiler error likeexpected ';' before 'return'.Fix:every statement ends with;. Check the lineabovethe error message.

❌ Mistake 2: Forgetting #include

main.c
Run Online
int main() {
    printf("Hello!\n");
    // compiler: "what is printf??"

Symptom:implicit declaration of function 'printf'.Fix:#include <stdio.h>at the very top.

❌ Mistake 3: Unmatched Braces or Quotes

main.c
Run Online
printf("Hello!\n);
// ← missing closing quote

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:

  1. Three lines— print your name, city, and favorite food on separate lines (hint: threeprintfcalls, or\nbetween them)
  2. 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
  3. Do some math— addprintf("5 + 3 = %d\n", 5 + 3);and see C calculate for you
  4. Ask a question— look ahead withscanf("%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

main.c
Run Online
#include <stdio.h>
int main() {
    // Print greeting to the screen
    printf("Hello, World!\
");
    return 0;
}

Key Takeaways, Best Practices & Memory Safety Rules

Best Practice & Key Takeaways
Always explicitly specify 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 questions
Q

Q: What happens if you omit `#include `?

A: The compiler generates an implicit function declaration warning or error because it doesn't know the signature and return type of `printf()`.
Q

Q: Is `void main()` standard in C?

A: No. Standard C defines `main()` as returning `int`. Using `void main()` is non-standard and causes compiler warnings or platform-specific undefined behavior.

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations