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


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 */


    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

$ 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!

Enjoyed this article? Stay informed by joining our newsletter!


You must be logged in to post a comment.

About Author

Tech Foorti is the first largest tech company that helps people to earn money by just writing articles.