A simple "Hello, World!" program in C:
/////////////////////////////////////////////////////////////
#include <stdio.h>
int main() {
// Print "Hello, World!" to the console
printf("Hello, World!\n");
return 0;
}
//////////////////////////////////////////////////////////////
Let's break down the code:
1. `#include <stdio.h>`: This line includes the standard input-output library (`stdio.h`) in our program. This library provides functions like `printf()` and `scanf()` which are used for input and output operations.
2. `int main() { ... }`: This is the main function of our program. All C programs must have a `main()` function, and execution of the program begins from here. The `int` before `main()` indicates that the function returns an integer value, usually 0 to indicate successful execution.
3. `printf("Hello, World!\n");`: This line prints "Hello, World!" to the console. The `printf()` function is used to print formatted output to the console. In this case, we're simply printing the string "Hello, World!" followed by a newline character (`\n`), which moves the cursor to the next line after printing.
4. `return 0;`: This line indicates the end of the `main()` function and returns an integer value of 0 to the operating system, indicating that the program terminated successfully.
To compile and run this program:
1. Save the code in a file named `hello.c`.
2. Open a terminal or command prompt.
3. Navigate to the directory where `hello.c` is saved.
4. Compile the program using a C compiler like `gcc`:
```
gcc hello.c -o hello
```
This command will generate an executable file named `hello`.
5. Run the executable:
```
./hello
```
You should see "Hello, World!" printed to the console.
0 Comments