C Programming Beginner’s Guide: Learn “Hello World” and Set Up Your Environment

1. Why Learn “Hello World” as a C Language Beginner

C is a fundamental programming language that serves as the backbone for embedded systems and software development. That’s why, when learning programming for the first time, you typically start by creating a simple program called “Hello World.” This program is the first step toward understanding the basic syntax and how to run C programs. In this article, we’ll explain how to understand the basics of C through “Hello World” and guide you through the process of writing and executing the code.

2. Setting Up Your Development Environment

Before you can start programming, you need to set up your development environment. Here, we’ll cover how to set up two common environments: “GCC” and “Visual Studio.”

2.1 Setting Up with GCC

GCC (GNU Compiler Collection) is an open-source compiler widely used on Linux and macOS. Follow these steps to easily install GCC and set up an environment for running C programs.

  • Installation on Linux/macOS:
  1. Open your terminal and enter the following command:
    sudo apt install gcc # For Linux xcode-select --install # For macOS
  2. Once installed, verify GCC by running:
    gcc --version

2.2 Setting Up with Visual Studio

Visual Studio is a development environment for using C on Windows. Follow these steps to set it up.

  • Installation on Windows:
  1. Download and install Visual Studio from Microsoft’s official website.
  2. During installation, select “Desktop development with C++” so you can create C language projects.
  3. Create a new project in Visual Studio, write your code, and run it.
侍エンジニア塾

3. “Hello World” Program Code Explained

Now, let’s create the “Hello World” program and explain its code in detail. Below is the basic code for “Hello World” in C.

#include <stdio.h>

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

3.1 Role of #include <stdio.h>

#include is a directive used to include external libraries in your program. stdio.h provides standard input/output functions, allowing you to use printf(). Without it, you cannot display text on the screen.

3.2 Meaning of int main()

In C, every program starts with the main() function. int is the return type, and returning 0 indicates that the program ended successfully.

3.3 Explanation of printf("Hello, World!\n")

The printf() function displays the specified string to the console. Here, it prints “Hello, World!” and \n adds a newline.

3.4 Role of return 0

return ends the function and sends a value back to the caller. In main(), return 0 is standard and indicates normal program termination.

4. How to Compile and Run

To run a C program, you need to compile its source code. Below are the compilation methods for GCC and Visual Studio.

4.1 Compiling with GCC

On Linux or macOS, compile your C program as follows:

  1. Save your code in a file (e.g., hello.c).
  2. Run the following command to compile:
    gcc -o hello hello.c ./hello
    This compiles the program and runs it using ./hello.

4.2 Compiling with Visual Studio

In Visual Studio, compile your program as follows:

  1. Create a project and enter your code.
  2. Click “Build” → “Build Solution” to compile.
  3. Click “Debug” → “Start Debugging” to run the program.

5. Common Errors and Troubleshooting

While creating programs, you may encounter errors. Here are common beginner mistakes and how to fix them.

5.1 Missing Semicolon

In C, omitting a semicolon ; at the end of a statement causes an error. For example, forgetting the semicolon after a printf() statement results in:

  • Error Message: error: expected ';' before '}' token
  • Solution: Add ; after printf("Hello, World!\n").

5.2 Typo in Function or Variable Names

Misspelling function or variable names is another common mistake. For example, writing prontf instead of printf will cause an error.

  • Error Message: error: 'prontf' undeclared (first use in this function)
  • Solution: Correct the spelling of the function name.

5.3 Compilation Errors

Various errors can occur during compilation. Read the error message carefully and fix the highlighted section to resolve the issue.

6. Practical Examples

Here are some variations of the “Hello World” program to produce more complex outputs, helping you deepen your understanding of C basics.

6.1 Output Using Variables

This example shows how to dynamically output information using variables:

#include <stdio.h>

int main() {
    int age = 25;
    printf("I am %d years old.\n", age);
    return 0;
}

Here, %d is used to display the integer variable age.

6.2 Multi-line Output

This code outputs multiple lines:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    printf("Let's start learning C.\n");
    return 0;
}

Here, \n inserts a line break to display the message on two lines.

7. Summary and Next Steps

The “Hello World” program is essential for understanding C’s basic structure. Through this simple program, you’ve learned the flow of a C program and how to use standard output. As a next step, you can explore basic features such as arithmetic operations, conditionals, and loops to create more complex programs.

年収訴求