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:
Explanation:
- #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.
- 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.
- 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.
- 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:
- Save the program as hello_world.c.
- Open a terminal window.
- Navigate to the directory where the hello_world.c file is located.
Run the following command to compile the program:
Run the following command to run the program:
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.