C Examples Printf Helloworld
## C Programming Example: Printing "Hello, World!"
In computer programming, the "Hello, World!" program is traditionally used as the very first exercise when learning a new language. It serves as a simple sanity check to verify that your compiler, development environment, and runtime are set up correctly.
This tutorial demonstrates how to use the standard library function `printf()` to output "Hello, World!" to the console in C.
---
## The "Hello, World!" Code
Below is the complete C source code to print "Hello, World!" to the screen.
```c
#include
int main()
{
// The string inside printf() must be enclosed in double quotes
printf("Hello, World!\n");
return 0;
}
```
### Output
```text
Hello, World!
```
---
## Code Explanation
Let's break down each line of the program to understand how it works:
### 1. `#include `
This is a preprocessor command. It tells the compiler to include the **Standard Input/Output header file** (`stdio.h`) before compiling the code. This file contains the declaration of the `printf()` function. Without this line, the compiler will not recognize `printf()`.
### 2. `int main()`
Every C program must have a `main()` function. It serves as the entry point of the program. Execution starts from the first line inside this function.
* `int` indicates that the function returns an integer value to the operating system upon completion.
### 3. Curly Braces `{ ... }`
The curly braces define the body of the `main()` function. All the code belonging to the function is written inside these braces.
### 4. `printf("Hello, World!\n");`
* `printf()` is a standard library function used to print formatted output to the console.
* The text to be printed must be enclosed in double quotes (`"Hello, World!"`).
* The `\n` is an escape sequence representing a **newline character**. It moves the cursor to the next line after printing the text.
* Every statement in C must end with a semicolon (`;`).
### 5. `return 0;`
This statement terminates the `main()` function and returns the value `0` to the operating system. A return value of `0` conventionally signifies that the program executed successfully without any errors.
---
## Key Considerations
* **Case Sensitivity:** C is a case-sensitive language. For example, `printf` is correct, but `Printf` or `PRINTF` will cause a compilation error.
* **Double Quotes:** Strings in C must be enclosed in double quotes (`"..."`). Single quotes (`'...'`) are reserved for single characters (e.g., `'A'`).
* **Semicolon Termination:** Forgetting the semicolon `;` at the end of statements is one of the most common syntax errors for beginners. Always ensure your statements are properly terminated.
YouTip