You are here

Hello world C

Hello world C

How to write a hello world 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.

#include <stdio.h>

int main()
{
  printf("Hello world\n");
  return 0;
}

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 the function.

The program's purpose is to get familiar with the syntax of the C programming language. In it, we have printed a particular set of words. To print whatever you want to, see C program to print a string.

Output:
Hello world C program output

Download Hello world C program.

C hello world using character variables

#include <stdio.h>

int main()
{
  char a = 'H', b = 'e', c = 'l', d = 'o';
  char e = 'w', f = 'r', g = 'd';

  printf("%c%c%c%c%c %c%c%c%c%c", a, b, c, c, d, e, d, f, c, g);

  return 0;
}

We have used seven-character variables, '%c' is used to display a character variable. See other efficient ways below.

We may store "hello world" in a string (a character array).

#include <stdio.h>

int main()
{
  char s1[] = "HELLO WORLD";
  char s2[] = {'H','e','l','l','o',' ','w','o','r','l','d','\0'};

  printf("%s %s", s1, s2);

  return 0;
}

Output:
HELLO WORLD Hello world

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

C Hello world a number of times

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

#include <stdio.h>

int main()
{
  int c, n;

  puts("How many times?");
  scanf("%d", &n);

  for (c = 1; c <= n; c++)
    puts("Hello world!");

  return 0;
}

Hello world in C indefinitely

#include <stdio.h>

int main()
{
  while (1)  // This is always true, so the loop executes forever
    puts("Hello World");

  return 0;
}

To terminate the program press (Ctrl + C).