hello world program in C as 1st program

You can write hello world as the first program in any text editor. You must have to save the file with (.c) extension. To learn any programming language, you must write the code in your own. Don’t try to copy and paste our code to your editor. Try your own and always play with your keyboard. However, to write your first c program, open your text editor and write the following code.

#include <stdio.h>    
int main(){    
printf("Hello World\n");    
return 0;   
} 

Now save this program using any name with .c extension. For example firstprogram.c, helloworld.c or even yourname.c and after then run this program.

first c program, c programming, c tutorial, competitive programming

Wow! You have successfully done your first C program!

Lets try to understand our first program

Include section : We use several keywords, functions and statements while writing a program. Some of such very used functions are printf(), scanf() etc. When we use the libraries of “stdio.h”, it includes the definition of these functions. Because in “stdio.h”, there have the definition of these functions. It is used for reading data from terminal and displaying the output there. The printf() and scanf() function is defined in “stdio.h”.

Main function : Your program begins from here. Main function is the starting point of your C program. This should be present in all C programs as your program won’t begin without main() function. We will learn more about function in our function chapter. So, just know it for now.

printf() : The printf function will print whatever you give between double quotation inside it. You can use format specifier inside printf() to print different types of variables. Some of such format specifiers are %d, %c, %f, %p etc. which we will learn later.

\n : It will take a new line after your output. It is good practice to write \n after every output otherwise it is said to avoid new line.

return 0 : This refers the return of main function. The return type for main function will always be zero. We will learn more about other return type of a function later.

What is the need of return type of main function? The compiler should know about your program that it has compiled successfully or not. For this compiler checks the return value of main function. If return value is 0 then it decide the program is successfully compiled. Otherwise it takes a decision that there is a problem, and this is the reason we have a return 0 statement at the end of the main function.

Hello world printing in some other way

Now, lets try to make a program which will print hello world in our console. Don’t be afraid if you don’t understand these programs. You will understand all of these after completing this C tutorial.

Now, lets try to make a program which will print hello world in our console. Don’t be afraid if you don’t understand these programs. You will understand all of these after completing this C tutorial.

Use string to print our first hello world

We will take every characters of “Hello world” inside a string then print that string. So, lets try to understand the following program.

#include <stdio.h>
int main(){
  char firstHello[] = "Hello world";
  char secondHello[] = {'H','e','l','l','o',' ','w','o','r','l','d','\0'};

  printf("%s\n%s\n", firstHello, secondHello);

  return 0;
}

This program will give two “Hello world” in our console. One is from the inside of firstHellow string and other is from secondHello array.

Using character variables for hello world

We will take every characters of “Hello world” to separate character variable and then print every variable using a space between Hello and world. So, lets try to understand given program.

#include <stdio.h>
int main(){
  char a = 'H', b = 'e', c = 'l', d = 'o', e = 'w', f = 'r', g = 'd';

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

  return 0;
}

Run this program and it will display “Hello world” to your console.

Many hello world by loop control statement

If you don’t understand this program, don’t worry because you may not be familiar with the loop yet. Think that you are playing with C programming.

You need not write the comment section after //. It is a comment and have not any function to your program. It is used to understand the program by readers of your code.

#include <stdio.h>
int main(){
  int i, num;

  printf("How many times you want to print hello world?");
  scanf("%d", &num);

  for (i = 1; i <= num; i++){    //This is a loop. You will know more later
    printf("Hello world!\n");
    }
  return 0;
}

Run the code. It will tell you to give an input number that how many times you want to print “Hello world”. Give the number and press the Enter button. Now, see the magic. We will discuss more about loop in our C loops chapter.

Printing hello world in infinite times

Here, we will use an infinite loop which will be true at every time. For this the program will never stop. It will continue until the space of your memory is fully occupied. 

#include <stdio.h>
int main(){
  while (1)  // This is an infinite loop which will never stop.
    printf("Hello World\n");

  return 0;
}

You should not try this program if your computer have a few space in RAM and if it is low configured.

This is our first page only. You many not understand many things from here. Don’t worry! You can understand them all after finishing this tutorial.

Best of luck!

Previous pageC Introduction

Next page:  Variables in C