Writing Your First C Program: Hello, World!
Course Title: Mastering C: From Fundamentals to Advanced Programming Section Title: Introduction to C and Development Environment Topic: Writing your first C program: Hello, World!
1. Introduction
Congratulations on setting up your development environment and learning the basic C syntax. Now, it's time to write your first C program. The traditional "Hello, World!" program is a great way to start your C programming journey. In this topic, we'll guide you through creating, compiling, and running your first C program.
2. The "Hello, World!" Program
The "Hello, World!" program is a simple C program that prints "Hello, World!" to the console. Here's the code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Let's break down this code:
#include <stdio.h>
: This line tells the compiler to include the standard input/output library (stdio.h
) which provides functions for input/output operations, such asprintf()
.int main()
: This is the entry point of the program, where execution begins. Themain()
function is a special function that is called when the program starts.printf("Hello, World!\n");
: This line uses theprintf()
function to print "Hello, World!" to the console, followed by a newline character (\n
).return 0;
: This line indicates the end of themain()
function and returns an exit status of 0, indicating that the program executed successfully.
3. Compiling and Running the Program
Once you've written the "Hello, World!" program, you'll need to compile it using a C compiler. Here are the steps to compile and run the program:
Using GCC (GNU Compiler Collection)
- Save the "Hello, World!" program in a file named
hello.c
. - Open a terminal or command prompt.
- Navigate to the directory where you saved
hello.c
. - Compile the program using the following command:
gcc hello.c -o hello
- Run the program using the following command:
./hello
(on Linux/Mac) orhello.exe
(on Windows)
Using Code::Blocks
- Create a new project in Code::Blocks by selecting File > New > Project...
- Choose Console application as the project type.
- Copy the "Hello, World!" code into the
main.c
file. - Compile and run the program by clicking the Build and run button (or press F9).
Using Visual Studio
- Create a new project in Visual Studio by selecting File > New > Project...
- Choose Empty Project as the project type under the Visual C++ section.
- Add a new source file named
main.c
by right-clicking on the project and selecting Add > New Item... - Copy the "Hello, World!" code into the
main.c
file. - Compile and run the program by clicking the Debug > Start Debugging button (or press F5).
4. Troubleshooting Common Issues
If you encounter any issues while compiling or running the program, here are some common solutions:
- Check for typos in the code.
- Ensure that the file is saved with the correct extension (
.c
for C programs). - Verify that the compiler is installed and configured correctly.
- Check for missing libraries or headers.
5. Conclusion
Congratulations on writing and running your first C program! You've taken the first step towards mastering C programming. In the next topic, we'll explore conditional statements, including if-else and switch-case statements.
What's Next?
- Conditional statements: if, else, switch: Learn how to control the flow of your program using conditional statements.
We encourage you to practice writing C programs to reinforce your understanding of the concepts. If you have any questions or need help, please feel free to comment below.
Images

Comments