C C++

"Hello, World!" Program in C


Introduction

The "Hello, World!" program is traditionally the first program that beginners write when learning a new programming language. Its purpose is simple: display the message Hello, World! on the screen.

Although this program is very small, it introduces several fundamental concepts of the C programming language, including the program structure, the main() function, header files, function calls, and the return statement.

Hello, World! Program

The following example shows a complete C program that prints Hello, World! to the console.

Example

#include <stdio.h>

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

Program Output

When you compile and run the program, it produces the following output:

Output

Hello, World!

Understanding the Program

Let's examine each part of the program to understand what it does.

The #include Directive

The first line of the program is:

Example

#include <stdio.h>

The #include directive tells the preprocessor to include the contents of the stdio.h header file before compilation.

The stdio.h header provides declarations for standard input and output functions, including printf().

Note: Without including stdio.h, most C compilers will report an error or warning when printf() is used.

The main() Function

Every C program starts execution from the main() function.

Example

int main(void)
{
    ...
}

The keyword int indicates that the function returns an integer value. The void parameter list means the function does not accept any arguments.

The printf() Function

Inside the main() function, the following statement prints text to the console.

Example

printf("Hello, World!\n");

The printf() function displays the string enclosed in double quotation marks.

The \n at the end of the string is the newline escape sequence. It moves the cursor to the beginning of the next line after printing the message.

The return Statement

The last statement in the program is:

Example

return 0;

The return 0; statement ends the main() function and returns the value 0 to the operating system.

A return value of 0 generally indicates that the program completed successfully.

Program Execution Flow

When this program runs, the following steps occur:

  1. The preprocessor includes the contents of stdio.h.
  2. The compiler compiles the source code into machine code.
  3. The operating system starts executing the main() function.
  4. The printf() function prints Hello, World!.
  5. The program returns 0 and terminates successfully.

Common Beginner Mistakes

  • Forgetting to include stdio.h.
  • Misspelling printf().
  • Using single quotes instead of double quotes for strings.
  • Forgetting the semicolon (;) at the end of a statement.
  • Missing one of the curly braces ({}).
Warning: C is case-sensitive. For example, Printf() and printf() are different identifiers.

Try Changing the Output

You can modify the string inside printf() to display any message you like.

Example

#include <stdio.h>

int main(void)
{
    printf("Welcome to C Programming!\n");
    return 0;
}

After compiling and running the program, the output becomes:

Output

Welcome to C Programming!

Summary

The Hello, World! program is a simple but important introduction to C programming. It demonstrates the basic structure of every C program and introduces several essential concepts that you will use throughout your learning journey.

From this example, you have learned:

  • How to include a header file using #include.
  • How the main() function serves as the program's entry point.
  • How to display text using printf().
  • How the newline character \n works.
  • Why return 0; is commonly used to indicate successful program execution.

With this foundation, you are ready to explore variables, data types, operators, and other core features of the C programming language.