Home » Programming Languages » C Programs » First C Program – “Hello World”

First C Program – “Hello World”

Let’s write a first C program which normally would be “helloworld” We begin with the text editor Vim.

The below command shows the syntax to create a new file with the name “helloworld” and extension “.c”

$ vim helloworld.c

The simple “helloworld” program is given below, we save and exit the Vim editor by pressing”:x” after the program is completed writing as shown below:

#include <stdio.h>

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

Compiling and executing the C program:

We use a utility known as GNU Compiler Collection or GCC to compile C programs.

$ gcc -o helloworld helloworld.c

Using the above command, we will compile the helloworld.c file created earlier into an executable C file. To execute the file and see the output on the terminal, we follow the next step:

$ ./helloworld
Hello World 

Below is the syntax for a simple C program:

header

return_type main(void) {
       body;
       return success/failure;
}

Parts of the C program:

Every C program contains minimum 3 sections as follows:

  • Header
  • main
  • Body

Header – We include the header “.h” files at the beginning of the C program.
.h header files contain the declarations of the functions used in the program. Here, we use the function “printf” declared in stdio.h and defined in c library libc.

main – is the entry point of every C program.
Every C program should contain one main for program execution.

The syntax adjacent to the main is like below,
return data type” main (void)
– return data type for main is “int” and it is used to indicate the success or failure of the program execution. The above program “return 0” is related to the return data type which returns integer 0.

void inside main(void) indicates main doesn’t accept any parameters right now from the command line, but there are ways to accept command line arguments using argc & argv to main which we will see later.

body – We can include anything in the body of a “C” program. This includes either a single printf or a complex program which is defined inside open & close curly brace brackets { … body … }
NOTE: Every statement inside the body ends with a “semicolon” “;”

printf(“Hello World \n”);
here, this function “printf” prints the string “Hello World” onto the terminal.
“\n” – indicates go to a new line, so using within printf it prints strings and goes to a new line, which makes displaying of output on terminal look good.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment