Create "Hello World" In C

CCAdvanced
Practice Now

Introduction

In this lab, you will learn how to create a basic "Hello World" program in the C programming language. The lab covers the fundamental structure of a C program, including headers and the main function. You will also install the GCC (GNU Compiler Collection) compiler, write a simple "Hello World" program, compile it, and execute the resulting binary. By the end of this lab, you will have a solid understanding of the basic steps involved in developing and running a C program.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("C")) -.-> c/FunctionsGroup(["Functions"]) c(("C")) -.-> c/UserInteractionGroup(["User Interaction"]) c(("C")) -.-> c/BasicsGroup(["Basics"]) c/BasicsGroup -.-> c/variables("Variables") c/BasicsGroup -.-> c/comments("Comments") c/FunctionsGroup -.-> c/function_declaration("Function Declaration") c/UserInteractionGroup -.-> c/user_input("User Input") c/UserInteractionGroup -.-> c/output("Output") subgraph Lab Skills c/variables -.-> lab-438286{{"Create #quot;Hello World#quot; In C"}} c/comments -.-> lab-438286{{"Create #quot;Hello World#quot; In C"}} c/function_declaration -.-> lab-438286{{"Create #quot;Hello World#quot; In C"}} c/user_input -.-> lab-438286{{"Create #quot;Hello World#quot; In C"}} c/output -.-> lab-438286{{"Create #quot;Hello World#quot; In C"}} end

Review C Program Structure (Headers And Main)

Embarking on the journey of C programming begins with understanding the fundamental building blocks of a program. Every programming language has its unique structure, and C is no exception. In this exploration, we'll unravel the essential components that form the backbone of a C program, making the seemingly complex world of coding more approachable for beginners.

When you first start programming, the blank screen can feel intimidating. However, by breaking down the code into digestible pieces, you'll quickly realize that programming is about understanding logical structures and building blocks. The code we'll examine serves as a perfect introduction to the core elements of C programming.

Open the WebIDE and create a new file named program_structure.c in the ~/project directory. We'll break down the essential elements of a C program.

Header And Main
#include <stdio.h>

int main() {
    printf("Hello C\n");
    return 0;
}

If you want to learn more about the WebIDE, check out the WebIDE Guide.

Diving into the code reveals the fundamental anatomy of a C program. Think of these components like the essential ingredients in a recipe - each plays a crucial role in creating a successful program. Let's explore these elements with the curiosity of a budding programmer.

Header Files (#include <stdio.h>) represent the gateway to additional functionality in your program. Much like how a toolbox provides specialized tools for different tasks, header files offer predefined functions and capabilities. The stdio.h header, standing for standard input/output, is particularly important as it enables basic input and output operations like printing text to the screen.

The main function (int main()) is the heart of any C program. Imagine it as the starting point of a journey, the first step in a sequence of instructions. Every C program must have this function, which serves as the entry point where the computer begins executing your code. The int return type is like a status report, telling the system whether the program completed successfully.

When you see return 0;, think of it as the program's way of saying, "I've finished my task without any problems." It's a simple yet powerful mechanism to communicate the successful completion of your program's execution.

Understanding these foundational elements transforms programming from a mysterious process to a logical, step-by-step approach. As you continue your programming journey, each line of code will become clearer, and you'll start to see the elegant simplicity behind complex software systems.

Install GCC Compiler On Your System

In this step, we'll walk through the process of installing the GCC compiler, which is essential for compiling and running C programs. While the LabEx environment typically comes with GCC pre-installed, we'll go through the installation process to provide a comprehensive learning experience.

In LabEx VM, the GCC compiler is pre-installed. This step is for educational purposes to understand the installation process.

When working with Linux-based systems like Ubuntu, installing software is typically done through package managers. The commands we'll use are simple yet powerful, allowing you to set up your development environment with just a few keystrokes.

Open the terminal in your WebIDE and run the following commands:

sudo apt update
sudo apt install -y gcc

Example output:

Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  gcc-11-base libgcc-11-dev libgcc1
Suggested packages:
  gcc-11-locales
The following NEW packages will be installed:
  gcc gcc-11-base libgcc-11-dev libgcc1
0 upgraded, 4 newly installed, 0 to remove and 18 not upgraded.
Need to get 0 B/2172 kB of archives.
After this operation, 6496 kB of additional disk space will be used.

These commands first update the package list and then install the GCC compiler. The -y flag automatically answers "yes" to any prompts, making the installation process smooth and uninterrupted.

Let's verify the GCC installation by checking its version:

gcc --version

Example output:

gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The version information tells you which specific version of GCC is installed on your system. This can be important for understanding compatibility and features available to you.

To further confirm the compiler works, let's run the program we created earlier:

cd ~/project
gcc program_structure.c -o program_structure
./program_structure

Example output:

Hello C

This sequence of commands demonstrates the typical workflow of compiling and running a C program. The gcc command compiles your source code, the -o flag specifies the output executable name, and ./ runs the compiled program.

If you see the "Hello C" message, your GCC compiler is working correctly. You've successfully installed GCC and compiled and run a C program. Congratulations on taking your first steps into the world of C programming!

Write A Basic "Hello World" Program

In this step, we'll revisit the classic "Hello World" program in C. It will help you review the basic structure of a C program, including headers, the main function, and the printf() function. This initial exercise is crucial for understanding how C programs are constructed and executed.

For newcomers to programming, creating your first program can feel both exciting and intimidating. Don't worry - every professional programmer started exactly where you are now, with a simple line of code that prints a greeting to the screen.

Open the WebIDE terminal and navigate to the project directory:

cd ~/project

Creating a new file is your first practical step in bringing your program to life. The process of creating and editing files is a fundamental skill in software development.

Create a new file called hello.c using the touch editor:

touch hello.c

Now, type the following code into the editor:

#include <stdio.h>

int main() {
    printf("Hello, World\n");
    return 0;
}

Tips: Practice writing C programs to improve your coding skills. The more you write, the better you'll get.

Each line of this simple program is a building block of C programming. Let's explore what's happening behind the scenes. The code might look short, but it contains several important programming concepts that you'll use throughout your coding journey.

Let's break down the code:

  • #include <stdio.h> includes the standard input/output library
  • int main() is the main function where the program starts
  • printf() prints text to the screen
  • \n creates a new line after printing
  • return 0; indicates the program completed successfully

Compiling transforms your human-readable code into instructions that a computer can understand. This process is a critical step in bringing your program to life.

Compile the program using GCC:

gcc hello.c -o hello

Example output:

(no output if compilation is successful)

Running the program is the moment of truth - where you see the result of your coding efforts. Each successful run is a small victory in your programming journey.

Run the compiled program:

./hello

Example output:

Hello, World

If you see the "Hello, World" message, congratulations! You've just written, compiled, and run your first C program. This achievement marks the beginning of your programming adventure, opening doors to more complex and exciting coding challenges.

Summary

In this lab, we first reviewed the fundamental structure of a C program, focusing on headers and the main function. We learned that header files provide additional functionality, and the main() function is the entry point of the program. We also installed the GCC (GNU Compiler Collection) compiler, which is essential for compiling and running C programs.