C Notes
Learn how to write, compile, and run your first C program. Complete Hello World tutorial with line-by-line explanation, compilation steps, and common errors for beginners.
Every programmer's journey begins with a simple program that prints "Hello, World!" on the screen. This tradition dates back to the 1978 book "The C Programming Language" by Brian Kernighan and Dennis Ritchie. In this tutorial, you'll write your very first C program, understand every single line of it, and learn how to compile and run it successfully.
Writing Your First C Program
Open your favorite text editor or IDE (VS Code, Notepad++, or even a plain terminal editor like nano) and type the following code. Save the file as hello.c — the .c extension tells the compiler that this is a C source file.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}That's it! These six lines are all you need for your first working C program. Let's break down what each line does.
Line-by-Line Explanation
Line 1: #include <stdio.h>
This is a preprocessor directive. It tells the compiler to include the contents of the Standard Input/Output header file (stdio.h) before compilation begins. This header file contains the declaration of the printf() function that we use to display text on the screen.
Think of it like importing a toolbox — you need to bring in the right tools before you can use them.
Line 2: Empty Line
Empty lines are ignored by the compiler. We use them to improve readability. Your code should be easy to read, both for yourself and for others.
Line 3: int main()
This is the main function — the entry point of every C program. When you run your compiled program, execution always starts from main(). The int before main means this function will return an integer value to the operating system when it finishes.
Line 4: printf("Hello, World!\n");
This is the statement that actually does the work. printf() is a library function that prints formatted text to the console. The \n at the end is an escape sequence that represents a newline character — it moves the cursor to the next line after printing.
Line 5: return 0;
This statement returns the value 0 to the operating system, indicating that the program executed successfully. By convention, returning 0 means "no errors," while any non-zero value indicates something went wrong.
Line 6: }
The closing curly brace marks the end of the main() function body.
Compiling the Program
C is a compiled language, which means you need to convert your human-readable source code into machine code before you can run it. Here's how to compile using GCC:
gcc hello.c -o helloThis command tells GCC to:
- Take
hello.cas input - Compile it into an executable
- Name the output file
hello(orhello.exeon Windows)
If there are no errors, this command produces no output — silence means success!
Running the Program
After successful compilation, run the executable:
On Linux/macOS:
./helloOn Windows:
hello.exeExpected Output:
Hello, World!
Congratulations! You've just written, compiled, and executed your first C program.
Understanding the Compilation Process
When you compile a C program, it goes through four stages:
| Stage | What Happens | Output |
|---|---|---|
| Preprocessing | Processes #include, #define directives | Expanded source code |
| Compilation | Converts C code to assembly language | Assembly file (.s) |
| Assembly | Converts assembly to machine code | Object file (.o) |
| Linking | Links object files with libraries | Executable binary |
You can observe each stage separately:
# Preprocessing only
gcc -E hello.c -o hello.i
# Compile to assembly
gcc -S hello.c -o hello.s
# Compile to object file
gcc -c hello.c -o hello.o
# Link to create executable
gcc hello.o -o helloCommon Variations of Hello World
Using puts() instead of printf()
#include <stdio.h>
int main() {
puts("Hello, World!");
return 0;
}The puts() function automatically adds a newline at the end, so you don't need \n.
Printing Multiple Lines
#include <stdio.h>
int main() {
printf("Hello, World!\n");
printf("Welcome to C Programming!\n");
printf("Let's learn together.\n");
return 0;
}Hello, World! Welcome to C Programming! Let's learn together.
Using Escape Sequences
#include <stdio.h>
int main() {
printf("Name:\tJohn\n");
printf("Age:\t25\n");
printf("She said, \"Hello!\"\n");
return 0;
}Name: John Age: 25 She said, "Hello!"
Common Escape Sequences in C
| Escape Sequence | Description |
|---|---|
\n | Newline |
\t | Horizontal tab |
\\ | Backslash |
\" | Double quote |
\0 | Null character |
\a | Alert (beep) |
\r | Carriage return |
Common Errors Beginners Make
1. Missing Semicolon
printf("Hello, World!\n") // ERROR: missing semicolonThe compiler will throw an error like: error: expected ';' before '}'
2. Wrong Header File Name
#include <studio.h> // ERROR: should be stdio.h3. Using Capital Letters for Keywords
Int Main() { // ERROR: C is case-sensitive
Printf("Hello\n"); // ERROR: should be printf
}4. Forgetting the Return Statement
While modern compilers may not always complain, it's best practice to always include return 0; in your main() function.
5. Saving with Wrong Extension
Make sure your file ends with .c and not .txt or .cpp. The extension matters for the compiler to recognize the language.
What Happens Behind the Scenes
When you execute ./hello, the operating system:
- Loads the executable into memory
- Finds the
main()function as the entry point - Executes statements sequentially from top to bottom
- The
printf()call triggers a system call to write text to the terminal return 0sends the exit status back to the OS- The OS reclaims the memory used by the program
You can check the exit status of the last command in the terminal:
./hello
echo $?Hello, World! 0
The 0 confirms that your program exited successfully.
Interview Questions
Q1: Why do we use #include <stdio.h> in a C program?
We include stdio.h because it contains the declarations (prototypes) of standard input/output functions like printf(), scanf(), puts(), and gets(). Without this header, the compiler wouldn't know about these functions and would throw an error or warning.
Q2: What is the significance of return 0 in the main function?
return 0 indicates successful program termination to the operating system. The OS uses this return value (called exit status) to determine whether the program ran successfully. A return value of 0 means success, while any non-zero value indicates an error. This is useful in shell scripts and automated processes.
Q3: Can a C program run without a main() function?
No, a standard C program cannot run without a main() function. The main() function is the mandatory entry point defined by the C standard. The linker looks for main as the starting address. However, in embedded systems or with platform-specific tricks, it's technically possible to have different entry points.
Q4: What is the difference between printf() and puts() in C?
printf() is a formatted output function that supports format specifiers (%d, %s, %f, etc.) and does not automatically append a newline. puts() simply outputs a string followed by an automatic newline and does not support format specifiers. puts() is slightly faster for simple string output because it doesn't need to parse format specifiers.
Q5: What happens if we write void main() instead of int main()?
While void main() may work on some compilers, it is not standard-compliant. The C standard (C99 and C11) specifies that main() should return int. Using void main() can lead to undefined behavior and portability issues. Always use int main() for standards-compliant code.
Summary
- Every C program starts execution from the
main()function #include <stdio.h>is needed for input/output functions likeprintf()- C programs must be compiled before execution using a compiler like GCC
- The compilation process involves preprocessing, compilation, assembly, and linking
return 0signals successful program termination- C is case-sensitive —
Mainis not the same asmain - Always save your C source files with the
.cextension - Use escape sequences like
\nfor newlines and\tfor tabs
Now that you've successfully run your first C program, you're ready to explore variables, data types, and more complex programs. The foundation you've built here — understanding compilation and program structure — will serve you throughout your C programming journey.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for First C Program - Hello World in C with Explanation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, setup, first, program, first c program - hello world in c with explanation
Related C Programming Topics