The Wayback Machine - https://web.archive.org/web/20240822135115/https://www.geeksforgeeks.org/c-hello-world-program/
Open In App

C Hello World Program

Last Updated : 31 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Write a C program to print “Hello, World!” on the console screen.

The “Hello World” program is the first step towards learning any programming language and also one of the simplest programs you will learn. All one needs to do is display the message “Hello World” on the screen.

C Program to Print “Hello World”

To print the “Hello World”, we will use the printf function from the stdio.h library. The following C program displays “Hello World” in the output screen:

C
// Simple C program to display "Hello World"

// Header file for input output functions
#include <stdio.h>

// Main function: entry point for execution
int main() {

    // writing print statement to print hello world
    printf("Hello World");

    return 0;
}

Output
Hello World

Explanation:

  • #include <stdio.h> – This line includes the standard input-output library in the program.
  • int main() – The main function where the execution of the program begins.
  • printf(“Hello, World!\n”); – This function call prints “Hello, World!” followed by a new line.
  • return 0; –This statement indicates that the program ended successfully.

Different Ways to Write a “Hello, World!” Program in C for Beginners

1. Basic Hello World

This is the simplest and most common way to write a “Hello, World!” program in C.

C++
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Output
Hello, World!

2. Using a Function to Print Hello World

You can create a separate function to print “Hello, World!”.

C++
#include <stdio.h>

// Function to print Hello, World!
void printHelloWorld() {
    printf("Hello, World!\n");
}

int main() {
    printHelloWorld();
    return 0;
}

Output
Hello, World!

3. Using a Macro

You can define a macro to print “Hello, World!”.

C++
#include <stdio.h>

#define HELLO_WORLD() printf("Hello, World!\n")

int main() {
    HELLO_WORLD();
    return 0;
}

Output
Hello, World!

4. Without Including stdio.h

Although not recommended for beginners, you can avoid including stdio.h and use system calls instead. This is more advanced and not as portable.

C++
int main() {
    write(1, "Hello, World!\n", 14);
    return 0;
}

Output
Hello, World!

5. Using Multiple Print Statements

You can break the message into multiple printf statements.

C++
#include <stdio.h>

int main() {
    printf("Hello, ");
    printf("World!\n");
    return 0;
}

Output
Hello, World!

6. Using Character-by-Character Printing

You can print each character of the string separately.

C++
#include <stdio.h>

int main() {
    char *str = "Hello, World!\n";
    while (*str) {
        putchar(*str++);
    }
    return 0;
}

Output
Hello, World!

7. Using puts() Function

The puts function can be used instead of printf for a simpler approach.

C++
#include <stdio.h>

int main() {
    puts("Hello, World!");
    return 0;
}

Output
Hello, World!

These are various ways to write a “Hello, World!” program in C. Each example demonstrates different aspects and functions of the language, providing multiple approaches for beginners to understand and explore.

NEXT TO READ:



Previous Article
Next Article

Similar Reads

Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
OpenMP | Hello World program
Prerequisite: OpenMP | Introduction with Installation GuideIn C/C++/Fortran, parallel programming can be achieved using OpenMP. In this article, we will learn how to create a parallel Hello World Program using OpenMP.STEPS TO CREATE A PARALLEL PROGRAM Include the header file: We have to include the OpenMP header for our program along with the stand
3 min read
Print "Hello World" in C/C++ without using any header file
Write a C/C++ program that prints Hello World without including any header file. Conceptually it's seems impractical to write a C/C++ program that print Hello World without using a header file of "stdio.h". Since the declaration of printf() function contains in the "stdio.h" header file. But we can easily achieve this by taking the advantage of C p
2 min read
Print Hello World without semicolon in C/C++
Every statement in C++ must end with a semicolon as per basics. However, unlike other languages, almost all statements in C++ can be treated as expressions. However there are few scenarios when we can write a running program without semicolon. If we place the statement inside an if/switch/while/macro statement with a blank pair of parentheses, we d
2 min read
Lex Program to remove comments from C program
Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) cc lex.yy.c -efl ./a.out Let’s see LEX program to accept string starting
1 min read
C Program to Print the Program Name and All its Arguments
The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the number of arguments in the command line and argv []
2 min read
C Program for Program to cyclically rotate an array by one
Given an array, cyclically rotate the array clockwise by one. Examples: Input : arr[] = {1, 2, 3, 4, 5} Output : arr[] = {5, 1, 2, 3, 4}Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. C/C++ Code // C++ code for program to // cyclically rotate an array by one # include &lt;iostream&gt; using namespace std; // Meth
2 min read
C program to detect tokens in a C program
As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keywords: Examples- for, while, if etc. 2) Identifier Exa
5 min read
C program to print a string without any quote (single or double) in the program
Print a string without using quotes anywhere in the program using C or C++. Note : should not read input from the console. The idea is to use macro processor in C (Refer point 6 of this article). A token passed to macro can be converted to a string literal by using # before it. C/C++ Code // C program to print a string without // quote in the progr
1 min read
C Program for Program for array rotation
Write a function rotate(arr[], d, n) that rotates arr[] of size n by d elements.   Rotation of the above array by 2 will make array Recommended PracticeRotate ArrayTry It!Method 1 (Rotate one by one): leftRotate(arr[], d, n)start For i = 0 to i &lt; d Left rotate all elements of arr[] by oneendTo rotate by one, store arr[0] in a temporary variable
4 min read
Output of the program | Dereference, Reference, Dereference, Reference....
Predict the output of below program #include&lt;stdio.h&gt; int main() { char *ptr = &quot;geeksforgeeks&quot;; printf(&quot;%c\n&quot;, *&amp;*&amp;*ptr); getchar(); return 0; } Output: g Explanation: The operator * is used for dereferencing and the operator &amp; is used to get the address. These operators cancel effect of each other when used on
1 min read
Quine - A self-reproducing program
A quine is a program which prints a copy of its own as the only output. A quine takes no input. Quines are named after the American mathematician and logician Willard Van Orman Quine (1908–2000). The interesting thing is you are not allowed to use open and then print file of the program. To the best of our knowledge, below is the shortest quine in
1 min read
C Program to list all files and sub-directories in a directory
#include &lt;stdio.h&gt; #include &lt;dirent.h&gt; int main(void) { struct dirent *de; // Pointer for directory entry // opendir() returns a pointer of DIR type. DIR *dr = opendir(&quot;.&quot;); if (dr == NULL) // opendir returns NULL if couldn't open directory { printf(&quot;Could not open current directory&quot; ); return 0; } // Refer http://pu
1 min read
C Program for Bubble Sort on Linked List
Given a singly linked list, sort it using bubble sort. Input : 10-&gt;30-&gt;20-&gt;5 Output : 5-&gt;10-&gt;20-&gt;30 Input : 20-&gt;4-&gt;3 Output : 3-&gt;4-&gt;20 C/C++ Code // C program to implement Bubble Sort on singly linked list #include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; /* structure for a node */ struct Node { int data; struct Node *n
3 min read
C Program to print contents of file
fopen() is used to open and fclose() is used to close a file in C #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // For exit() int main() { FILE *fptr; char filename[100], c; printf(&quot;Enter the filename to open \n&quot;); scanf(&quot;%s&quot;, filename); // Open file fptr = fopen(filename, &quot;r&quot;); if (fptr == NULL) { printf(&quot;Ca
1 min read
C program to delete a file
The remove() function in C/C++ can be used to delete a file. The function returns 0 if the file is deleted successfully, Otherwise, it returns a non-zero value. The remove() is defined inside the &lt;stdio.h&gt; header file. Syntax of remove()remove("filename");ParametersThis function takes a string as a parameter, which represents the name of the
2 min read
C Program to Split a String into a Number of Sub-Strings
Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter.  In C:  // Splits str[] according to given delimiters. // and returns next token. It needs to be
1 min read
C program to invert (making negative) an image content in PGM format
Given an image in PGM format and the task is to invert the image color (making negative) content in PGM format. Prerequisite: c-program-to-write-an-image-in-pgm-formatPGM image represents a gray scale graphic image. PGM is the abbreviation of Portable Gray Map. This image file contains one or more PGM images file. Significance of data chunks: The d
3 min read
C Program for Counting Sort
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence. Algorithm: Step 1: StartStep 2 : Find Largest Data From Array.Step 3 : Set All Array Element
3 min read
C Program for Min Cost Path
Given a cost matrix cost[][] and a position (m, n) in cost[][], write a function that returns cost of minimum cost path to reach (m, n) from (0, 0). Each cell of the matrix represents a cost to traverse through that cell. Total cost of a path to reach (m, n) is sum of all the costs on that path (including both source and destination). You can only
3 min read
C Program for Pancake sorting
Given an unsorted array, sort the given array. You are allowed to do only following operation on array. flip(arr, i): Reverse array from 0 to i C/C++ Code /* C program for Pancake Sorting */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* Reverses arr[0..i] */ void flip(int arr[], int i) { int temp, start = 0; while (start &lt; i) { temp = ar
2 min read
C++ Program for Recursive Insertion Sort
Insertion sort is a simple and efficient sorting algorithm that is often used for small lists or as a building block for more complex algorithms. It works by dividing the input list into two parts: a sorted sublist of items that has already been traversed, and an unsorted sublist of items remaining to be sorted. The algorithm begins with the second
3 min read
C Program for Iterative Merge Sort
Following is a typical recursive implementation of Merge Sort that uses last element as pivot. C/C++ Code /* Recursive C program for merge sort */ #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; /* Function to merge the two haves arr[l..m] and arr[m+1..r] of array arr[] */ void merge(int arr[], int l, int m, int r); /* l is for left index and r
3 min read
C Program for Iterative Quick Sort
C/C++ Code // An iterative implementation of quick sort #include &amp;lt;stdio.h&amp;gt; // A utility function to swap two elements void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } /* This function is same in both iterative and recursive*/ int partition(int arr[], int l, int h) { int x = arr[h]; int i = (l - 1); for (int j = l; j &amp;lt;
4 min read
C/C++ Program for nth multiple of a number in Fibonacci Series
Given two integers n and k. Find position the n'th multiple of K in the Fibonacci series. Examples: Input : k = 2, n = 3 Output : 9 3'rd multiple of 2 in Fibonacci Series is 34 which appears at position 9. Input : k = 4, n = 5 Output : 30 5'th multiple of 4 in Fibonacci Series is 832040 which appears at position 30. An Efficient Solution is based o
4 min read
C program to write an image in PGM format
PGM stands for Portable Gray Map. Saving a 2D array in C as images in PNG, JPG or other formats would need a lot of effort to encode the data in the specified format before writing to a file. However, Netpbm format offers a simple solution with easy portability. A Netpbm format is any graphics format used and defined by the Netpbm project. The port
4 min read
C Program for Basic Euclidean algorithms
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors. C/C++ Code // C program to demonstrate Basic Euclidean Algorithm #include &lt;stdio.h&gt; // Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } //
1 min read
C Program for Extended Euclidean algorithms
GCD of two numbers is the largest number that divides both of them. A simple way to find GCD is to factorize both numbers and multiply common factors. C/C++ Code // C program to demonstrate working of extended // Euclidean Algorithm #include &amp;lt;stdio.h&amp;gt; // C function for extended Euclidean Algorithm int gcdExtended(int a, int b, int* x,
1 min read
C Program For Lower Case to Uppercase and Vice-Versa in a File
Given a text file(gfg.txt), our task is to convert all the lowercase characters of the file into uppercase. ExamplesInput: (content inside file gfg.txt) An extensive classroom programme by GeeksforGeeks to build and enhance Data Structures and Algorithm concepts Output: (content inside file gfg.txt) AN EXTENSIVE CLASSROOM PROGRAMME BY GEEKSFORGEEKS
4 min read
C program to create a pendulum clock using graphics
In this article, it is discussed how to design a Pendulum Clock in the C programming language using Graphics. Approach: Create two rectangles, one inside the other, using the rectangle() function to act as the outer outline of the clock &amp; the other is the inner outline of the clock.Color the space between the two rectangles brown using setfills
3 min read
three90RightbarBannerImg