C Programs Tutorials | IT Developer
IT Developer

C Programming - C Hello World Program



Share with a Friend

C Programming - C Hello World Program

The "Hello, World!" program is the simplest C program that demonstrates the basic structure of a C program and outputs the text "Hello, World!" to the console. Below is the code, followed by a detailed explanation:

Code Example: Hello World in C

Example:

#include <stdio.h> // Preprocessor directive to include the standard input/output header int main() { printf("Hello, World! \n"); // Print "Hello, World!" to the console return 0; // Return 0 to indicate successful execution }

Explanation of the Code

  1. #include <stdio.h>
    • This preprocessor directive includes the Standard Input/Output library.
    • The library provides functions like printf() and scanf() that are used for input and output operations.
  2. int main()
    • This is the main function, where the execution of the program begins.
    • It returns an integer value to the operating system. A return value of 0 typically signifies successful execution.
  3. printf("Hello, World!\n");
    • The printf() function is used to display the string "Hello, World!" on the console.
    • The \n adds a newline after the output, moving the cursor to the next line.
  4. return 0;
    • This statement indicates that the program has completed successfully.
    • The integer value 0 is returned to the operating system.

Output

When you run the program, the output will be:

Hello World!

Steps to Run the Program

  1. Write the Code: Save the code in a file with the .c extension (e.g., hello_world.c).
  2. Compile the Program: Use a C compiler like gcc to compile the program:

gcc hello_world.c -o hello_world

  1. Run the Executable: Execute the compiled program:

bash

./hello_world

  1. View the Output: The message Hello, World! will be displayed on the console.

Key Points

  • Portability: The program can be compiled and executed on various platforms.
  • Basic Syntax: The example demonstrates the basic syntax and structure of a C program.
  • First Program: It is often the first program written by beginners to get started with C programming.

This simple program is a foundational step toward understanding more complex concepts in C.