Introduction
This lab is designed to help you create your first C program, the classic “Hello, World!” program.
This lab is designed to help you create your first C program, the classic “Hello, World!” program.
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:
gcc --version
If a version number is returned, you have GCC installed. Otherwise, you can follow the installation instructions for your operating system.
mkdir ~/project
This will create a directory named projects under your home directory where we will save all our files related to this project.
~/project
directory using the following command:cd ~/project
main.c
using the following command:nano main.c
This will open a new file named main.c in the nano editor.
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
This program simply prints “Hello, World!” to the console.
Ctrl + X
, then Y
, then Enter
.gcc main.c -o hello
This will compile main.c
and output an executable named hello
in the current directory.
./hello
You should see the output Hello, World!
printed to the console.
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;
}
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!