C Hello World: Your First Program – A Step‑by‑Step Guide
Below is a classic "Hello World" program written in C, followed by a detailed explanation of each component.
#include <stdio.h> // Pre‑processor directive
int main(void) // Main function declaration
{
printf("Hello World"); // Output the string to the console
return 0; // Terminate the program
}
1. Pre‑processor Directive
The #include <stdio.h> directive tells the compiler to incorporate the Standard Input/Output library, where printf is defined. Including the appropriate header file is mandatory before using any function from that library.
Custom header files can also be included in the same way: #include <file‑name.h>. Place all #include statements at the top of your source file.
2. The main Function
Every C program must contain a main function. Common signatures include:
- int main(void)
- int main(int argc, char *argv[])
Using int main(void) signifies that the program does not accept command‑line arguments and will return an integer exit status. A return value of 0 indicates successful execution.
3. Code Structure
- Curly braces (
{ }) delimit the body ofmainand any other functions. - Each statement ends with a semicolon (
;). - The
printffunction prints the specified text to the console.
4. Running a C Program
Below are the typical steps to create, compile, and run a C program in a common IDE:
- Create a new project and choose a C/C++ source file.
- Configure the file path and name (ending with
.c). - Save the file and click "Build & Run".
Once compiled, the output window should display:
Hello, World!
5. Summary
- The
mainfunction is essential for every C program. - Header files must be included at the top of the source file.
- All C programs follow a consistent structural pattern.
C Language
- Building Your First FPGA: LED Blink Tutorial (VHDL & Verilog)
- C# Hello World – Building Your First C# Application
- Java Hello World: Your First Program
- Your First VHDL Program: A Step‑by‑Step Hello World Tutorial
- C++ Hello World Tutorial: Step‑by‑Step Code, Setup & Explanation
- C# Hello World: Building Your First Console Application
- calloc() in C: Zero‑Initialized Memory Allocation and a Practical Example
- Java Hello World: Step‑by‑Step Guide to Writing Your First Java Program
- Hello World: Building Your First Python Application
- Understanding Python's Main Function: A Practical Guide to def main()