C Hello World Program (Using Functions, Loops, Arrays, and More)
The C language Hello World program is the classic starting point for every beginner learning to code.
In this tutorial, you'll learn various ways to print "HELLO, WORLD!"
in C — from basic syntax to variations using loops, arrays, and functions.
Introduction
If you're looking to understand how to write a Hello World Program in C, this guide will help you learn:
-
The basic structure of a C program
-
How to use
printf()
to print output -
Different methods to print Hello World using loops, arrays, and functions
Hello World Program in C
// HELLO WORLD PROGRAM IN C USING MAIN FUNCTION
#include <stdio.h>
// It's the main function of the program
int main()
{
// Step-1 Display a label message
printf("C HELLO WORLD PROGRAM\n");
// Step-2 Print the actual Hello World message
printf("HELLO, WORLD!\n");
return 0;
}
Output
C HELLO WORLD PROGRAM
HELLO, WORLD!
Using a Function (User-defined)
// HELLO WORLD PROGRAM IN C USING A USER-DEFINED FUNCTION
#include <stdio.h>
// Function to display messages
void showMessage()
{
printf("C HELLO WORLD PROGRAM\n");
printf("HELLO, WORLD!\n");
}
int main()
{
// Call the showMessage() function to print the messages
showMessage();
return 0;
}
Using a Loop (For Loop)
// HELLO WORLD PROGRAM IN C USING FOR LOOP
#include <stdio.h>
int main()
{
// Create a string array of messages
char *messages[] = {
"C HELLO WORLD PROGRAM",
"HELLO, WORLD!"
};
// Loop through the array and print each message
for (int i = 0; i < 2; i++)
{
printf("%s\n", messages[i]);
}
return 0;
}
Using an Array Only (String Array)
// HELLO WORLD PROGRAM IN C USING STRING ARRAY
#include <stdio.h>
int main()
{
// Declare an array of strings
char *msg1 = "C HELLO WORLD PROGRAM";
char *msg2 = "HELLO, WORLD!";
// Print both messages
printf("%s\n", msg1);
printf("%s\n", msg2);
return 0;
}
Using Conditional Statements (If statement)
// HELLO WORLD PROGRAM IN C USING IF STATEMENT
#include <stdio.h>
int main()
{
int showMessage = 1;
if (showMessage)
{
printf("C HELLO WORLD PROGRAM\n");
printf("HELLO, WORLD!\n");
}
return 0;
}
Common Mistakes
-
Missing semicolon
;
at the end ofprintf()
statements -
Forgetting
#include <stdio.h>
-
Not returning an integer from
main()
FAQs
Q1: Do I always need to use main()
in C?
Yes, main()
is the entry point of a C program.
Q2: What does printf()
do?
It prints output to the standard output (usually the console).
Q3: Can I write C code without functions?
You can write simple code in main()
only, but using functions is recommended for cleaner structure.