Writing Your First C Program: Step-by-Step Hello World Tutorial

Writing First C Program

Writing First C Program

I still remember the exact moment my very first C program printed “Hello, World!” to the screen. It felt anticlimactic at first — just two words on a black terminal — but that small success taught me more about how computers actually work than months of reading theory ever did. In this tutorial, I want to walk you through writing your first C program from absolute scratch, explaining every single line, every symbol, and every step in between, so that by the end you’re not just copying code — you actually understand what it’s doing.

Before You Start: What You’ll Need

To follow along, you need three things:

  1. A text editor (anything from Notepad to VS Code works fine).
  2. A C compiler installed on your machine (GCC is the most common).
  3. A terminal or command prompt to run commands.

If you haven’t installed a compiler yet, I’d recommend pausing here and setting that up first — there’s a dedicated guide for installing GCC on Windows, Mac, and Linux that walks through it step by step. For this tutorial, I’ll assume GCC is already available and that typing gcc --version in your terminal returns a version number instead of an error.

Step 1: Writing the Code

Open your text editor and create a new file named hello.c. The .c extension matters — it tells your compiler (and yourself, later) that this is C source code, not just plain text.

Type the following exactly as shown:

#include <stdio.h>

int main(void) {
    printf("Hello, World!\n");
    return 0;
}

I want to emphasize: type it out yourself rather than copy-pasting. The act of typing each character forces you to notice details — the semicolons, the parentheses, the capitalization — that are easy to skim over otherwise.

Save the file in a folder you can easily navigate to from your terminal, for example Documents/c-projects/hello.c.

Step 2: Understanding Every Single Line

Let’s slow down and go through this program line by line, because each part matters more than its brevity suggests.

#include <stdio.h>

This line tells the preprocessor to include the Standard Input/Output header file, which contains the declarations for functions like printf() and scanf(). Without this line, the compiler wouldn’t know what printf is, and you’d get an error saying something like “implicit declaration of function ‘printf’.”

The angle brackets < > tell the compiler to look for this header in the system’s standard include directories, rather than in your local project folder (which would use double quotes instead, like "myheader.h").

int main(void)

This declares the main function — the mandatory entry point of every C program. Let’s break down each part:

{ and }

These curly braces mark the beginning and end of the function’s body — everything the function actually does lives between them. C is very particular about matching braces; every opening brace needs a corresponding closing one, and mismatched braces are one of the most common sources of confusing compiler errors for beginners.

printf("Hello, World!\n");

This is a function call to printf, which stands for “print formatted.” It takes a string (enclosed in double quotes) and prints it to the standard output — normally your terminal screen.

The \n inside the string is an escape sequence representing a newline character. Without it, your terminal’s next prompt would appear directly after “Hello, World!” on the same line, rather than on a fresh line below it.

Notice the semicolon at the end. In C, semicolons terminate statements — nearly every line of executable code needs one. Forgetting it is probably the single most common beginner mistake, and it usually produces an error message pointing to the next line rather than the missing semicolon itself, which can be confusing until you know to expect it.

return 0;

This ends the main function and returns the value 0 to the operating system, conventionally signaling that the program completed successfully. If something had gone wrong, you might return a non-zero value instead, like return 1;.

Step 3: Compiling Your Program

Open your terminal, navigate to the folder containing hello.c, and run:

gcc hello.c -o hello

Let’s unpack this command:

If everything is typed correctly, this command produces no output at all — silence means success in the world of compilers. If you made a typo, you’ll see an error message pointing to the line and column where the compiler got confused.

Step 4: Running Your Program

Now run the compiled executable:

./hello

On Windows Command Prompt, you’d instead type:

hello.exe

Output:

Hello, World!

Congratulations — you’ve just written, compiled, and executed your first C program. That single line of output represents an entire pipeline working correctly: your text file was read by the compiler, translated into machine code, linked against the C standard library, loaded into memory by the operating system, and executed by your CPU.

Common Beginner Mistakes at This Stage

I made every one of these mistakes myself when I started, so don’t feel bad if you hit one too:

  1. Forgetting the semicolon after printf(...), producing an error like expected ';' before 'return'.
  2. Mismatched quotes, like writing printf("Hello, World!\n); — missing the closing quote. The compiler often reports a confusing cascade of errors after this kind of mistake.
  3. Using void main() instead of int main(void). While some compilers tolerate this, it’s non-standard and can cause portability issues; int main(void) is the correct, standards-compliant form.
  4. Forgetting #include <stdio.h>, leading to a warning or error about an “implicit declaration” of printf.
  5. Running the source file directly instead of the compiled executable — typing ./hello.c instead of ./hello and getting a “permission denied” or “cannot execute” error, since .c files aren’t executable programs, just plain text.
  6. Case sensitivity confusion — C is case-sensitive, so Main is not the same as main, and the compiler won’t recognize Main as the required entry point.

Expanding Your First Program

Once “Hello, World!” works, it’s worth immediately extending it slightly, because a single printf call doesn’t exercise much of the language. Here’s a slightly richer version that introduces variables and user input:

#include <stdio.h>

int main(void) {
    char name[50];

    printf("Hello, World!\n");
    printf("What is your name? ");
    scanf("%49s", name);

    printf("Nice to meet you, %s!\n", name);

    return 0;
}

Sample interaction:

Hello, World!
What is your name? Sarah
Nice to meet you, Sarah!

This small addition introduces several new concepts at once: declaring a character array to hold text, reading input with scanf(), and using a format specifier (%s) to insert a variable’s value into a printed string. I limited the input to 49 characters (%49s) specifically to avoid writing past the end of the 50-byte array — a subtle but important safety habit in C, where buffer overflows are a real and common risk.

What’s Actually Happening Behind the Scenes

It’s worth understanding, even briefly, what your computer does after you type ./hello:

  1. The operating system loads the compiled binary into a fresh region of memory.
  2. It sets up the program’s stack, heap, and other memory segments.
  3. A small startup routine runs before your main() function, setting up the C runtime environment.
  4. Your main() function executes, calling printf, which in turn makes a system call to write text to the terminal’s standard output stream.
  5. When main() returns, the runtime performs cleanup and passes your return value back to the operating system as the program’s exit status.

You can actually verify this exit status yourself:

./hello
echo $?

Output:

Hello, World!
0

That final 0 confirms the value we returned from main().

Best Practices Even at This Early Stage

Real-World Relevance of This Exercise

“Hello, World!” might feel trivial, but the underlying skills you just practiced — writing syntactically correct C, invoking a compiler with the right flags, and running the resulting binary — are exactly the same skills used on massive production codebases. Whether you’re compiling a tiny teaching example or a component of the Linux kernel, the fundamental compile-and-run workflow doesn’t change; only the scale and complexity of the source code does.

Common Interview Questions Related to This Topic

Frequently Asked Questions

Q: Why does my terminal say “command not found” when I try ./hello? This almost always means the compilation step failed or didn’t produce a file named exactly hello. Double-check your gcc command completed without errors, and confirm you’re in the correct directory.

Q: Can I name my program file something other than hello.c? Yes, the filename can be anything as long as it ends in .c. Just remember to match the compile command to your actual filename.

Q: Do I always need \n at the end of my printf statements? Not strictly, but it’s good practice, since it keeps your terminal output readable by ensuring each print statement starts on a fresh line.

Q: Why do some tutorials use void main() instead of int main(void)? Older or non-standard tutorials sometimes use void main(), but it isn’t part of the official C standard. Sticking with int main(void) keeps your code portable and standards-compliant across different compilers and operating systems.

Summary and Key Takeaways

Writing your first C program is a small but genuinely meaningful milestone. It walks you through the entire lifecycle of a C program — from source code to compiled binary to running process — in miniature.

Key points to remember:

References

Every experienced C programmer started exactly where you are right now, staring at a blinking cursor and a two-word greeting on the screen. The habits you build in this very first exercise — careful reading of errors, frequent compiling, and genuine curiosity about what each line does — are the same habits that will carry you through far more complex programs down the road.

Exit mobile version