Writing First CProgram - Hello World Example

The "Hello, World!" program in C is a simple introductory program that displays the text "Hello, World!" on the screen. It's often the first program that new programmers write in a new programming language. Here's how to create and understand the "Hello, World!" program in C:

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

Explanation:

  1. #include <stdio.h>: This line is a preprocessor directive that tells the C compiler to include the standard input/output library (stdio.h). This library provides functions like printf for input and output operations.
  2. int main() { ... }: This is the main function where the program starts its execution. The int before main specifies that the function returns an integer value. The { ... } encloses the body of the function.
  3. printf("Hello, World!\n");: This line is the heart of the program. It uses the printf function to print the text "Hello, World!" to the screen. The \n represents a newline character, which moves the cursor to the next line after printing.
  4. return 0;: The return statement is used to exit the main function and return a status code to the operating system. In this case, it returns 0 to indicate that the program executed successfully.

Execution:

This is the simplest C program that prints the message "Hello, world!" to the screen. You can compile and run this program using the following steps:

  1. Save the program as hello_world.c.
  2. Open a terminal window.
  3. Navigate to the directory where the hello_world.c file is located.

Run the following command to compile the program:

gcc hello_world.c -o hello_world

Run the following command to run the program:

./hello_world

This will print the message "Hello, world!" to the screen.

Conclusion

The "Hello, World!" program in C is a basic introductory program that displays the message "Hello, World!" on the screen. It serves as a starting point for learning C programming and demonstrates fundamental concepts such as function structure, output with printf, and program execution flow.