Hello World, C!

CCBeginner
Practice Now

Introduction

This lab is designed to help you create your first C program, the classic “Hello, World!” program.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123263{{"`Hello World, C!`"}} c/data_types -.-> lab-123263{{"`Hello World, C!`"}} c/operators -.-> lab-123263{{"`Hello World, C!`"}} c/function_declaration -.-> lab-123263{{"`Hello World, C!`"}} end

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:

  1. 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.

  1. 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.

  1. Change into the ~/project directory using the following command:
cd ~/project

Creating the program

  1. Create a new file called main.c using the following command:
nano main.c

This will open a new file named main.c in the nano editor.

  1. 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.

  1. Save the file and exit the editor. In nano, you can do that by pressing Ctrl + X, then Y, then Enter.

Compiling and running the program

  1. 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.

  1. Run the program by entering the following command:
./hello

You should see the output Hello, World! printed to the console.

Full Code

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!

Other C Tutorials you may like