A Hello World C program on Linux

By | 01/09/2012

The C language has very close relation with Linux operating system. Most of the Linux kernel code is written in C language. On the user space side, most of the commands and utilities are written in C language. So one of the very first thing one should learn as part of Linux development is to write, compile and execute a hello world C program on Linux.

In this article, we will understand how to write, compile and run a hello world C program on Linux in 3 easy steps.

A Hello World C program on Linux

1. Writing a hello world C program

Consider the following source code :

#include<stdio.h>

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

The above source code is of a basic program that prints “Hello World”. Please note that in Linux, a C program can be written in any graphical text editor like ‘gedit’ or a command line editor like ‘vi’. Lets save the above code in a file named helloworld.c

2. Compiling the Hello World program

As the source code is in place, the next step is to compile the source code. A compiler is used to compile source code on any platform. On Linux the most popular C compiler is ‘gcc’. This compiler comes by default loaded with almost all of the Linux distributions. FYI, I use Linux Mint.

Any-ways, Lets open the Linux shell and compile our program.

Run the following command in the directory where you have saved the helloworld.c file.

$ gcc -Wall helloworld.c -o helloworld

The above gcc command compiles the source code (and Links the object file) to produce an executable named ‘helloworld’ in the same directory as helloworld.c file. The flag -o is used to specify the output executable file name. If -o is not used then gcc names ‘a.out’ as the output executable. The flag -Wall is used to turn on all types of warnings while compiling the code.

As already discussed, the output of above command is an executable named ‘helloworld’.

3. Run the executable

To run the executable produced in above step, use the following command :

$./helloworld

The reason for using a ./ before the executable name is to tell the shell environment that the executable ‘helloworld’ resides in the current directory.

The above command should produce the following output :

Hello World

So this way a basic C program is written, compiled and executed on Linux.

2 thoughts on “A Hello World C program on Linux

  1. Leigh Bowden

    I’m using Linux Mint. Why does yours work and mine doesn’t?

    leigh@MINT-SERVER:~ > cat hello.c
    #include

    int main(void)

    {
    printf(“Hello world.\n”);
    return 0;
    }
    leigh@MINT-SERVER:~ > gcc -Wall hello.c -o hello
    hello.c:1:19: fatal error: stdio.h: No such file or directory
    #include
    ^
    compilation terminated.
    leigh@MINT-SERVER:~ >

    Reply
    1. Carsten Holtkamp

      Many of the distributed Linux based Operating Systems are Desktop User orientated and home development is not _really_ encouraged.

      In this case you are missing the header files from The GNU C Library which is part GNU-Linux C runtime environment.

      gcc complained:
      fatal error: stdio.h: No such file

      gcc checked the include pathes and simply did not find the file called stdio.h
      Which stands for standardized Input and Output, the .h is a convention.
      The Library is there, otherwise your system wouldn’t run at all. But the file that needs to be included (that is why they are also named includes) to your programtextfile aka source-code, is missing.

      The # sign ‘signals’ a preprocessor line, which is parsed by GCC’s pre-processor named cpp (C-pre-processor)

      BTW: Your include line, is missing the name of the header file.

      Don’t worry gcc will include the standard stuff anyways, except the math header.

      With -Wall, which means Warn all, but it actually doesen’t warn about everything. It should be named walot – which in turn would state that it warns in the most usual cases.

      How to figure out where the file should be?
      Either you need to remember that 2 of the most common directories for includeable headers are:
      /usr/local/include
      /usr/include

      And they are checked in this order.

      Or you make use of the –verbose / -v commandline argument of GCC.

      gcc -v -Wall foo.c -o foo

      You gonna need /usr/include/stdio.h

      How to figure out, in which package the file is?
      Mint is Debian based, it uses .deb packages from the Ubuntu package pool.
      LMDE 2 would be a Mint Linux which uses Debian package pool :-)

      The program apt-file pulls the contents of all your activated Package Sources aka Repositories.

      Install it:
      sudo apt-get install apt-file

      Build the local DB
      apt-file update

      then search for the file:
      apt-file search /usr/include/stdio.h

      It should respond:
      libc6-dev: /usr/include/stdio.h

      So, install the package:
      apt-get install libc6-dev

      Then you can the list package content if you want:
      dpkg -L libc6-dev | more

      Now you can compile your program.

      In this case you can type:
      $ make foo

      Further recommended reading:
      An Introduction to GCC – written by Brian Gough.
      It is free as non-printed version.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *