≡ Menu

How To Write, Compile and Execute C Program on Unix OS [With Hello World Example]

Question: I would like to understand the basics of how to write, compile and execute a C program on Linux OS. Can you explain it with a simple example?

Answer:  In this article, let us review very quickly how to write a basic Hello World C program and how to compile *.c program on Linux or Unix OS.

1. Write a Hello World C Program

Create the helloworld.c program using a Vim editor as shown below.

$ vim helloworld.c

/* Hello World C Program */

#include<stdio.h>

main()
{
    printf("Hello World!");

}

2. Make sure C Compiler (gcc) is installed on your system

Make sure gcc is installed on your system as shown below.

$ whereis cc
cc: /usr/bin/cc /usr/share/man/man1/cc.1.gz

$ which cc
/usr/bin/cc

$ dpkg -l | grep gcc
ii  gcc                                        4:4.3.3-1ubuntu1                          The GNU C compiler
ii  gcc-4.3                                    4.3.3-5ubuntu4                            The GNU C compiler
ii  gcc-4.3-base                               4.3.3-5ubuntu4                            The GNU Compiler Collection (base package)
ii  gcc-4.3-doc                                4.3.3-5ubuntu4                            Documentation for the GNU compilers (gcc, go
ii  gcc-4.3-locales                            4.3.3-5ubuntu4                            The GNU C compiler (native language support
ii  gcc-4.3-multilib                           4.3.3-5ubuntu4                            The GNU C compiler (multilib files)
ii  lib64gcc1                                  1:4.3.3-5ubuntu4                          GCC support library (64bit)
ii  libgcc1                                    1:4.3.3-5ubuntu4                          GCC support library

3. Compile the helloworld.c Program

Compile the helloworld.c using cc command as shown below. This will create the a.out file.

$ cc helloworld.c

$ ls -l
-rw-r--r-- 1 ramesh ramesh   71 2009-08-28 14:06 helloworld.c
-rwxr-xr-x 1 ramesh ramesh 9152 2009-08-28 14:07 a.out

4. Execute the C Program (a.out)

You can either execute the a.out to see the output (or) rename it to some other meaningful name and execute it as shown below.

$ ./a.out
Hello World!

$ mv a.out helloworld

$ ./helloworld
Hello World!
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • al mic September 4, 2009, 2:49 am

    Do you know why this appear:
    $ cc helloworld.c
    helloworld.c:3:10: error: #include expects “FILENAME” or
    helloworld.c: In function ‘main’:
    helloworld.c:7: warning: incompatible implicit declaration of built-in function ‘printf’

    ?

  • nardi September 4, 2009, 3:13 am

    I guess the compile-error is kind of easter egg from you =).

    $ cc helloworld.c
    helloworld.c:3:9: error: #include expects “FILENAME” or
    helloworld.c: In function ‘main’:
    helloworld.c:7: warning: incompatible implicit declaration of built-in function ‘printf

    Just to explain the first error (on line 3 of the file): We didn’t tell the #include directive “What” to include. It expects some header file. The one we probably want is stdio.h, for it includes the declaration of the printf function, which we use on line 7 and which is the one the compiler is complaining about in the second error (crying the declaration can’t be found in other words =).
    As it is System header file we add it in (don’t know how they’re called in eng =).

    I call this “great time to patch your first program” =))
    This is the patch file helloworld.patch:
    3c3
    #include

    To apply this simply type:
    $ patch helloworld.c < helloworld.patch

    Then compile the file again and enjoy =)

  • Rich Hunter September 4, 2009, 6:57 am

    I’m not a programmer. I tested your c program which did not compile. I searched google and found this line of yours may be incorrect:
    #include
    which should be
    #include
    With “#include” the program compiled.

    Rich

  • Rich Hunter September 4, 2009, 7:06 am

    Hello again.

    I don’t think I mistyped that second #include. I think your web site removes Here it is in parenthesis (studio.h) just in case it is removed in the above sentence.

    Rich

  • Rich Hunter September 4, 2009, 7:14 am

    Confirmed. Your web site removes angle brackets, aka diamond brackets, aka cone brackets or aka chevrons. I had to look that up too.

  • Rich Hunter September 4, 2009, 7:24 am

    By the way, I am using Firefox 3.0.12 on a newly installed Centos 5.3. Perhaps other web browsers will show the angle brackets.

  • scott September 4, 2009, 9:12 am

    stdio.h doesn’t appear in the listing of helloworld.c, probably interpreted as an html tag.

  • Ramesh Natarajan September 4, 2009, 12:28 pm

    @All,

    I’ve corrected it now. Thanks a lot for pointing out the issue.

    You all are right. It’s because of less-than and greater-than symbol in stdio.h did not get displayed properly in the blog post.

  • al mic September 4, 2009, 12:50 pm

    hey, that is great!
    thank you all!

  • kt October 4, 2010, 12:21 pm

    when I comepile a sourcefile, the error message keeps appearing and says that there is an implicit declaration of function printf. I don’t know how to fix it. Please help!

  • Anonymous October 25, 2011, 3:28 pm

    Thanks! I’ve been looking for a clear, short and meaningful tutorial and at least I found it!

  • deepak kumar January 2, 2012, 6:42 am

    helpful thnx

  • Raj January 16, 2013, 11:54 am

    nice thing to know….

  • Balajilp March 15, 2013, 12:37 am

    super. very nice!

  • Ehan Chang April 5, 2013, 7:37 pm

    good work! Thangks.

  • Ehan Chang April 5, 2013, 7:45 pm

    I rewrite the example as follow to avoide a trival proble:

    printf(“Hello World!\n”)
    /* ^^ */

    I insert a new line indicator to C.

  • santosh June 24, 2013, 2:00 am

    please let me know diffrent beetween windows and centos C ++ cammand

    i an using centos and i am training c ++ in windows when i run any scrip but not working let me know what i do

  • suriyarasu January 28, 2014, 3:04 am

    how to run unix programme

  • triveni July 23, 2015, 9:54 am

    i am not getting difference between turbo and unix .why to use $ and vi editor and so on.

  • Shrey Kaushal March 3, 2016, 10:19 pm

    Hey guys !!
    If suppose , I want to run a program of c++ .
    What am, I suppose to do as if after includinmg the header file”iostream”
    it still doesn’t work !!

  • Suman Dutta May 3, 2016, 9:47 am

    Can u say why the error segmentation fault(core dumped) error occurs during run time