Introduction
This lab is designed to help you create your first C program, the classic “Hello, World!” program.
Setting up the environment
Before we start writing code, we need to make sure our environment is set up properly. Follow the steps below to make sure you have everything you need:
- Open the terminal or command prompt and enter the following command to check if you have a C compiler installed:
gcc --version
If a version number is returned, you have GCC installed. Otherwise, you can follow the installation instructions for your operating system.
- Create a new directory for this project using the following command:
mkdir ~/project
This will create a directory named projects under your home directory where we will save all our files related to this project.
- Change into the
~/projectdirectory using the following command:
cd ~/project
Creating the program
- Create a new file called
main.cusing the following command:
nano main.c
This will open a new file named main.c in the nano editor.
- Type the following code into the editor:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
This program simply prints “Hello, World!” to the console.
- Save the file and exit the editor. In nano, you can do that by pressing
Ctrl + X, thenY, thenEnter.
Compiling and running the program
- Compile your code using the following command:
gcc main.c -o hello
This will compile main.c and output an executable named hello in the current directory.
- Run the program by entering the following command:
./hello
You should see the output Hello, World! printed to the console.
Full code of the program
Congratulations! You have written, compiled, and run your first C program. You are now ready to move on to more advanced topics. Here is the full code of your main.c file:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
Summary
In this lab, you learned how to create and run a simple C program that prints a message to the console. You also learned the basics of compiling and running C programs on the command line. Keep practicing and exploring the C language, and you will become a proficient C programmer in no time!



