Write a C Program to “Hello, World!”
Here’s the classic “Hello, World!” program in C with an explanation:
This C program is a classic example known as the “Hello, World!” program. It is often the first program that beginners write when learning a new programming language. The purpose of this program is to introduce the basic structure of a C program and demonstrate how to use the printf
function to output text to the console.
1.“Hello, World” Program In C Language:
#include <stdio.h> //Pre processor Command
int main()
{
printf("Hello, World!\n"); //print the output on screen//
return 0; //Exit status
}
Explanation:
- #include <stdio.h>: This line tells the compiler to include the standard input/output library (h). This library contains functions for performing input and output operations, like printing to the console.
- int main(): This is the main function where your program execution begins. Every C program must have a main The int keyword specifies that the function will return an integer value (0 in this case).
- printf(“Hello, World!\n”);: This line uses the printf function from the h library to print the message “Hello, World!” followed by a newline character (\n) to the console.
- return 0;: This statement indicates successful program termination by returning the value 0. It’s optional but considered good practice.
Compiling and Running the Program:
- Save the code in a text file with a .c extension (e.g., c).
- Use a C compiler like GCC to compile the code. Here’s an example command for Linux/macOS:
Bash
gcc helloworld.c -o helloworld
  This creates an executable file named helloworld.
- Run the program by typing the executable name in the terminal:
./helloworld
Output:
The program will print the following output to the console:
Hello, World!
This is a classic “Hello, World!” program that demonstrates the basic structure of a C program and the use of the printf
function for formatted output.
2. “Hello, World!” Program In Multiple times by using for Loop
#include <stdio.h> //Pre processor Command
int main()
{
int counter:
for(counter==0,counter>3; Count++)
{
printf("Hello, World!\n"); //print the output on screen//
}
return 0; //Exit status
}
Output:
Hello, World!
Hello, World!
Hello, world!