Introduction
In the world of computer science, there is a legendary tradition: the very first program you write in any new language must display the words "Hello, World!" on the screen. It is a rite of passage for every software engineer.
Now that you have your compiler and VS Code set up from our previous chapter, it is time to write your first C program. However, we are not just going to copy and paste code. As an aspiring engineer, you need to understand the anatomy of the code. Why do we use certain brackets? What do those specific words mean?
In this guide, we will dissect the "Hello World" program line-by-line so you understand exactly how C communicates with your computer.
The Code: Your First C Program
Open VS Code, create a new file named hello.c, and type the following code exactly as it appears. Do not forget the semicolons!
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Code Output
When you compile and run this code, your terminal will display:
Hello, World!
Line-by-Line Explanation
At first glance, this might look like gibberish. Let us break it down into four simple parts.
1. #include <stdio.h> (The Header File)
This is a preprocessor command. The word include tells the compiler to grab an external file and bring it into our program. stdio.h stands for Standard Input Output.
We need this file because C does not know how to print text to the screen by default. The instructions for printing (and reading keyboard input) live inside stdio.h. If you remove this line, the computer will throw an error because it won't recognize the printf command later on.
2. int main() { ... } (The Main Function)
Every C program must have a main() function. This is the starting point of your application. When you run your program, the computer immediately looks for main() and executes whatever is inside its curly brackets { }.
The int before it stands for "integer" (a whole number). It means that when this function finishes its job, it will hand a number back to the operating system to report its status.
3. printf("Hello, World!\n"); (The Output Command)
printf is a built-in function used to display information on the screen. The text you want to display must be wrapped in double quotes " ".
You might notice the \n at the end. This is a special character called a "newline." It tells the computer to press Enter and move the cursor to the next line after printing. Finally, notice the semicolon ; at the end. In C, a semicolon acts like a full stop at the end of an English sentence. It tells the compiler that the instruction is complete.
4. return 0; (The Exit Status)
Because we started our main function with int, we must return an integer at the end. Returning 0 is a universal signal to the operating system (like Windows or Linux) that the program ran successfully without any errors. If the program crashed, it would return a non-zero number.
Common Mistakes for Beginners
- Missing Semicolons: Forgetting the
;at the end ofprintforreturn 0is the #1 reason beginner programs fail to compile. - Case Sensitivity: C is strictly case-sensitive.
PrintforPRINTFwill not work; it must be completely lowercase:printf. - Wrong Brackets: Using parentheses
( )or square brackets[ ]instead of curly braces{ }for the main function body.
Best Practices
- Indentation: Notice how
printfandreturn 0;are pushed to the right (indented). This is not required by the computer, but it makes the code much easier for humans to read. - Meaningful File Names: Always save your files with a descriptive name ending in
.c(e.g.,hello.c, notprogram1.txt).
Interview Questions
Q: Can a C program run without a main() function?
Answer: No. The main() function is the mandatory entry point of any C program. The compiler will generate an error if it is missing.
Q: What does stdio.h contain?
Answer: It contains the declarations for standard input and output functions, such as printf() and scanf().
MCQs
Q1. Which symbol is used to terminate a statement in C?
A) Colon ( : )
B) Semicolon ( ; ) (Correct)
C) Period ( . )
D) Comma ( , )
Q2. What does \n do in the printf function?
A) Prints the letter n
B) Creates a new line (Correct)
C) Stops the program
D) Nothing
Practice Questions
- Modify the code to print your own name instead of "Hello, World!".
- Write a program that prints "Hello" on one line and "World" on the next line using a single
printfstatement.
Mini Assignment
Create a C program that prints a short introduction about yourself. It should include your name, your college or school name, and your favorite programming language. Make sure each piece of information is printed on a separate line.
Conclusion
Congratulations! You have officially written, compiled, and executed your first C program. You now understand the basic skeleton that every single C application uses, from simple calculators to massive operating systems.
In our next tutorial, Chapter 5: Comments, Tokens, and Best Practices, we will explore how to leave notes in your code and break down the smallest individual building blocks (tokens) of the C language. Keep practicing, and we will see you in the next lesson!
📚 Recommended Reading for Aspiring Developers
Expand your foundational tech knowledge with these premium guides from NeoGyan:
- Developer Tools: Now that you are writing code, it is time to learn how to save and track your versions. Read our guide: What is Git and Why Should Every Developer Learn It?
- Language Comparisons: Curious about other popular languages? Check out Python vs JavaScript: Which Should You Learn?
- Tech Careers: Thinking about how a degree affects your coding career? We break it down in The Skills vs Degree Debate: What Matters More?
- Understanding Systems: C interacts heavily with backend systems. Learn the basics of data transfer in What is API? Explained Simply.
- The OS Environment: The C language built Linux. Learn more about it in What is Linux? Beginner's Guide.