C "Hello, World!" Program

Hello World!

A "Hello, World!" program is usually the first program written by people who are beginners in writing codes. It is a very simple program which displays the message "Hello, World!". It is probably the simplest program to illustrate a few basic syntaxes of a programming language. This article covers the C programs to print "Hello World!".






1. Algorithm for "Hello, World!"


1. Print "Hello, World!"





2. Pseudocode for "Hello, World!"


1. Procedure print():

2. Print "Hello, World!"

3. End Procedure





3. Time complexity for "Hello, World!"


Time Complexity: O(1)





4. C Program & output for "Hello, World!"

Code has been copied
// Header file 
#include <stdio.h>

// main() function 
int main() 
{
    // prints hello world 
    printf("Hello, World!");

    return 0;
}

Output


Hello, World!


  • The goal of the above program is to display a string as the output. In this case "Hello World!" is the string. A string is a set of characters. It can be anything like a name, message, a set of numbers, or any combination of symbols.
  • #include is a preprocessor statement which tells the compiler to include the content of the header file stdio.h into the code.
  • A single line comment starts with a pair of slash (//). Comments are optional for a code. It helps the programmers to indicate the purpose of the code segments.
  • The stdio.h (standard input and output) file contains the printf() function.
  • The execution of the program starts from the main() function.
  • The printf() function displays "Hello, World!" text on the screen. To use the printf() function, it is necessary to write #include <stdio.h> at the beginning of the program; otherwise, the compiler can not compile the program.
  • The execution of return 0 statement implies that the program has run successfully without any error.



5. Print "Hello, World!" without using semicolon

Generally in a C program a semicolon ( ; ) is required after the printf() function. But there are a few tricks to print "Hello World!" without using a semicolon after this function. The ways are described below.


5.1. Use if in C Program to print "Hello, World!" without using semicolon

Code has been copied
#include <stdio.h>

int main() {
  // prints hello world using 
  // if statement
  if (printf("Hello, World!")) {

  }

  return 0;
}

Output


Hello, World!




5.2. Use switch in C Program to print "Hello, World!" without using semicolon

Code has been copied
#include <stdio.h>

int main() {
    // prints hello world using switch
    switch (printf("Hello, World!")) {

    }

    return 0;
}

Output


Hello, World!




5.3. Use while loop in C Program to print "Hello, World!" without using semicolon

Code has been copied
#include <stdio.h>

int main() {
    // prints hello world using while
    while (!printf("Hello, World!")) {

    }

    return 0;
}

Output


Hello, World!


  • printf("Hello World!") returns true to the while loop in the above program. A while loop keeps getting executed repeatedly unless the condition becomes false.
  • Inside the while loop, a not ( ! ) operator is placed before the printf() function to negate the true value to false so that we can avoid an infinite loop.



5.4. Use macro in C Program to print "Hello, World!" without using semicolon

Code has been copied
#include <stdio.h>
 // declare an macro
#define x printf("Hello World!")

int main() {
    // prints hello world using macro
    if (x) {

    }

    return 0;
}

Output


Hello, World!


  • Here, x is the name of the macro, and it is replaced by the expression printf("Hello World!") in the program at compile time.