Post

Chapter 2: Writing and Compiling a 'Hello, World!' Program

This chapter teaches how to write, compile, and run a simple C program (the classic Hello World example).

Chapter 2: Writing and Compiling a 'Hello, World!' Program

Table of Contents

2.1 Writing Your First C Program

To write a C program, you typically use a text editor to create a file with the .c extension. The simplest C program is one that prints “Hello, World!” to the screen. Here is an example:

1
2
3
4
5
6
#include <stdio.h>

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

Let’s break down this program:

  • #include <stdio.h> tells the compiler to include the standard input/output library, which provides the printf function.
  • int main() { ... } defines the main function where execution of the program begins. Every C program must have a main function.
  • Inside main, we call printf("Hello, World!\n"); which prints the text Hello, World! followed by a newline (\n).
  • return 0; signals that the program finished successfully.

Save this code in a file named hello.c.

2.2 Compiling and Running the Program

After writing the code, you need to compile it into an executable program and then run it. Assuming you have the GNU C Compiler (gcc) installed, you can compile the program with:

1
gcc hello.c -o hello

This command tells gcc to compile hello.c and output an executable named hello. If there are no errors, you can run the program by typing:

1
./hello

You should see:

1
Hello, World!

If there are syntax errors (like missing semicolons or braces), the compiler will report them. Fix any errors in your code and compile again.

2.3 Understanding the Code

It is important to understand every line of the Hello World program. Here’s a quick recap:

  • #include <stdio.h>: Includes the standard I/O library for printing.
  • int main() { ... }: Defines the main entry point of the program. int means this function returns an integer.
  • printf("Hello, World!\n");: Calls the function to print text. The \n is a special character that moves to a new line.
  • return 0;: Ends the main function and returns 0 to indicate success.

By understanding this basic program, you are ready to write your own C code and move on to more complex examples.

2.4 Exercises

  1. Write a C program that prints Hello, [Your Name]! to the screen. Replace [Your Name] with your actual name.
  2. Modify the Hello World program so that it prints two lines of text (for example, two greetings on separate lines).
  3. The following code has errors. Identify and fix them:
1
2
3
4
5
#include <stdio.h>
int main {
    printf("Hello, World!\n")
    return 0;
}
  1. What happens if you omit return 0; at the end of the main function? (Hint: Try compiling and running without it.)

2.5 Solutions

2.5.1 Solution 1

Here is an example solution to print your name:

1
2
3
4
5
6
#include <stdio.h>

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

Explanation: Replace Alice with your name. This program will print Hello, Alice! on the screen.

2.5.2 Solution 2

To print two lines, you can use two printf calls or include \n in the string twice:

1
2
3
4
5
6
7
#include <stdio.h>

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

Explanation: This program prints Hello, World! on the first line and Welcome to C programming! on the second line.

2.5.3 Solution 3

The code with errors was:

1
2
3
4
5
#include <stdio.h>
int main {
    printf("Hello, World!\n")
    return 0;
}

There are two errors: missing parentheses after main and missing semicolon after the printf line. The fixed code is:

1
2
3
4
5
#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}

Explanation: int main should be int main(), and printf("Hello, World!\n") needs a semicolon at the end of the line.

2.5.4 Solution 4

If you omit return 0;, most modern C compilers will still compile and run the program. The C standard (C99 and later) specifies that reaching the end of main without a return is equivalent to return 0;. However, it is good practice to include return 0; explicitly to indicate successful completion.

2.6 What’s Next?

In the next chapter, you’ll learn about variables, data types, and how to handle input/output in C.

Next Chapter → Variables, Data Types, and I/O

This post is licensed under CC BY 4.0 by the author.