You are here

C hello world program

How to write a hello world program in C language? To learn a programming language, you must start writing programs in it, and this could be your first C program. Let's have a look at the program first.

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   printf("Hello world\n");
  6.   return 0;
  7. }

Library function printf is used to display text on the screen, '\n' places the cursor at the beginning of the next line, "stdio.h" header file contains the declaration of printf function.

Purpose of this program is to get familiar with the syntax of C programming language. In this program we have printed a particular set of words, to print whatever you want to, see C program to print a string.

Output of program:
Hello world C program output

Download Hello world C program.

C hello world program using character variables

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   char a = 'H', b = 'e', c = 'l', d = 'o';
  6.   char e = 'w', f = 'r', g = 'd';
  7.  
  8.   printf("%c%c%c%c%c %c%c%c%c%c", a, b, c, c, d, e, d, f, c, g);
  9.  
  10.   return 0;
  11. }

As you can see we have used seven character variables in the program and '%c' is used to display a character variable, it is just another method. See easy and efficient ways to do the same in the following programs.

We may store "hello world" in a string (A character array) and then print it.

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   char s[] = {'H','e','l','l','o',' ','w','o','r','l','d','\0'};
  6.  
  7.   printf("%s\n", s);
  8.  
  9.   return 0;
  10. }

Output of program:
Hello world

If you didn't understand this program don't worry as you may not be familiar with the strings yet.

C program to print hello world a number of times

Using a for loop we can display it a number of times.

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int c, n;
  6.  
  7.   printf("How many times you want to display it?\n");
  8.   scanf("%d", &n);
  9.  
  10.   for (c=1; c<=n; c++)
  11.      printf("Hello world!\n");
  12.  
  13.   return 0;
  14. }

Displaying hello world indefinitely

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   while (1)  // This is always true so the loop will execute forever
  6.     printf("Hello World\n");
  7.  
  8.   return 0;
  9. }

While loop will execute forever as while(1) is always true. To terminate the program press (Ctrl + C).