Compile C Hello World Program – POFTUT

Compile C Hello World Program


C is a popular programming language for new programmers. There are some rituals while starting to learn some programming language which is called Hello World Example or Hello World or Hello Worl in C. In this tutorial, we will learn how to code the Hello World program in C, compile and run it from the command line.

Example Hello World Program

We will use the following Hello World source code which is very simple. We will print to the standard output the message Hello, Poftut!. There are also some comments about the application like /* I am C developer */.

#include <stdio.h> 
 
int main() { 
 
           /* I am C developer */ 
 
           printf("Hello, Poftut! \n"); 
               
           return 0; 
}
  • #include <stdio.h> is a library which provides required functions for our program. Libraries provide functionalities required in applications. For example, if we need some math functions like sin, cos, etc. there is two way to solve this problem. One way is to implement these function by writing them scratch which is a very tricky and time-consuming job. Another way is using existing libraries that provide these as functions or other ways in a very efficient way.
  • int main() { … } is our programs entrance function. By default, C applications start running from`main` function. Here int is not important for us but we will look it future chapters.
  • /* I am C developer */ is not part of code it is called comment. Comments are not included in program executable they just reside in source code. Comments are a very useful way to make our application readable and understandable.
  • printf("Hello, Poftut! \n"); is the actual code part here use the function `printf()` and provide a text which will be printed to the standard output.
LEARN MORE  How To Download, Compile and Install Custom Linux Kernel Manually In Ubuntu, Debian, Mint, Kali, CentOS?
Example Hello World Program
Example Hello World Program

Compile Hello World Program

Now our code is ready to compile and create an executable. Compiling will convert our source code to an executable. Executable means a file that can be run on the Operating System. We will use GCC. First, we save our code to a file named hello.c . And now we can simply call GCC like below by providing our source code file.

$ gcc hello.c

Now an executable file name a.out is created by GCC. We can simply run this file like below.

$ ./a.out
Compile Hello World Program
Compile Hello World Program

Set Name For Hello World Executable File

As we see the above created executable file is named a.out . This is an ugly way. In big projects, this will fail the compilation. We can set a name for the newly created executable file with -o parameter of GCC.

$ gcc -o hello hello.c
$ ./hello
Set Name For Hello World Executable File
Set Name For Hello World Executable File

3 thoughts on “Compile C Hello World Program”

  1. this code fails as you are not returning an integer to the main function on exit, the final line of code should read:

    return 0;

    Reply

Leave a Comment