Introduction
Writing your first C program is an exciting milestone in your coding journey! This is where theory meets real action.
Why Study This Topic?
Think of C programming like building with LEGO blocks. Your very first program is the foundation block — once you place it right, you can build anything from small gadgets to rockets (well, almost!). Creating and running a basic program builds your confidence and sets the stage for bigger projects.
What Will Be Covered?
- How to write a basic “Hello, World!” program
- How to save and compile the program
- How to run your first executable file
- Common beginner mistakes and how to fix them
Steps to Write Your First C Program
Open Your IDE or Text Editor
- You can use Code::Blocks, Visual Studio Code, or even a simple Notepad.
Type the Program
Here’s a classic “Hello, World!” program:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
Explanation:
#include <stdio.h>
: Tells the compiler to include standard input/output functions.int main()
: Main function where the execution begins.printf
: Prints text to the screen.return 0;
: Signals that the program ended successfully.
Saving the File
- Save your file with a
.c
extension.
Example:hello.c
Compiling the Program
Using GCC:
Open your terminal or command prompt and run:
gcc hello.c -o hello
This command tells GCC to:
- Compile
hello.c
- Output a file named
hello
(orhello.exe
on Windows)
🚀 Running the Program
- On Windows:
hello
- On macOS/Linux:
./hello
✅ If everything is set up correctly, you should see:
Hello, World!
Common Beginner Mistakes
- Missing semicolon (
;
) at the end of a statement. - Incorrect filename or file extension.
- Forgetting to include the necessary header files.
- Typing errors in commands or syntax.
Summary
- Writing your first C program involves creating a file, adding basic syntax, compiling it, and running the executable.
- The “Hello, World!” program introduces key C concepts like functions, statements, and syntax structure.
- Attention to detail (like semicolons!) matters a lot in C.
Learning Outcomes
After this topic, you’ll be able to:
- Write and save a simple C program.
- Compile code using a C compiler like GCC.
- Execute your compiled program from the terminal.
- Troubleshoot basic syntax and compilation errors.
Common Interview Questions
Q1: What is the entry point of a C program?[Zoho]
A: The main()
function.
Q2: What library must be included to use printf
in C?[IBM]
A: #include <stdio.h>
Q3: What happens if you forget a semicolon in C?[Accenture]
A: Compilation will fail with a syntax error.
Q4: What does the return 0;
statement do in C?[TCS]
A: It signals successful program completion to the operating system.
Practice Exercises & Quizzes
Exercises
- Write a program that prints your name.
- Write a program that prints two lines of text.
Take Quiz: