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

by Ramesh Natarajan on September 4, 2009

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!

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 18 comments… read them below or add one }

1 al mic September 4, 2009 at 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’

?

2 nardi September 4, 2009 at 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 =)

3 Rich Hunter September 4, 2009 at 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

4 Rich Hunter September 4, 2009 at 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

5 Rich Hunter September 4, 2009 at 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.

6 Rich Hunter September 4, 2009 at 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.

7 scott September 4, 2009 at 9:12 am

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

8 Ramesh Natarajan September 4, 2009 at 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.

9 al mic September 4, 2009 at 12:50 pm

hey, that is great!
thank you all!

10 kt October 4, 2010 at 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!

11 Anonymous October 25, 2011 at 3:28 pm

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

12 deepak kumar January 2, 2012 at 6:42 am

helpful thnx

13 Raj January 16, 2013 at 11:54 am

nice thing to know….

14 Balajilp March 15, 2013 at 12:37 am

super. very nice!

15 Ehan Chang April 5, 2013 at 7:37 pm

good work! Thangks.

16 Ehan Chang April 5, 2013 at 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.

17 santosh June 24, 2013 at 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

18 suriyarasu January 28, 2014 at 3:04 am

how to run unix programme

Leave a Comment

Previous post:

Next post: