How do I compile C program and create an executable file under Linux or UNIX operating systems?


Sample Unix C ProgramYou need GNU project C and C++ compiler for compiling C program and creating an executable file. Most Unix and Linux (*BSD) user start compiling their C program by the name cc. [donotprint]

Tutorial details
Difficulty level Easy
Root privileges No
Requirements C compiler
Est. reading time 2 minutes
[/donotprint]But you can use gcc command to compile program. First make sure you have gcc C compiler installed:

Make Sure Compiler Is Installed On a Unix-like System

Type the following which command or type command or command command to verify that gcc is installed:
$ type -a gcc
$ command -V gcc
$ which gcc

Sample outputs:


Find out version of gcc, run:
$ gcc --version
Sample outpust:

gcc (GCC) 4.0.3 20060212 (prerelease) (Debian 4.0.2-9)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO


The syntax is as follows to compile a C program on a Unix-like operating system:
gcc program.c -o program-output
cc program.c -o program-output
make program

Writing Your First C Program Under a Linux / UNIX-like system

Use a text editor such as vi or gedit or nano to create a C program called first.c:
$ vi first.c
Type the following lines (program):

#include <stdio.h>
int main(void){
  printf("My first C program\n");
  return 0;

How Do I Compile My C Program?

To compile C program first.c, and create an executable file called first, enter:
$ gcc first.c -o first
$ cc first.c -o first
To execute program first, enter:
$ ./first

My first C program

However, both FreeBSD and Linux support direct make (GNU make utility to maintain groups of programs) command on C program without writing a make file. Remove, first program using the rm command:
$ rm first
$ make first


cc   first.o   -o first

Execute program first:
$ ./first
Please note that above hack works with GNU/make only.

See also:
This entry is 9 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

79 comments… add one
  • Javed Akhter Dec 16, 2015 @ 1:06

    I have question.
    1) The source code of a C program file is stored in a file with the ____ extension.

    2)____ is an example of how you can declare a string in C.

    Please can you send me Answer.

  • M.BABYSATHYA. Mar 1, 2016 @ 7:22

    give example prg in linux using c prg and how run

  • Chris Nov 24, 2016 @ 18:44

    This was amazing! Thanks!

  • Dave B Jan 19, 2017 @ 21:52

    In the tutorial example :-

    $ make first

    The output to screen is actually…
    cc first.c -o first

    Note the .c not .o as shown in the tutorial.

    Otherwise works as advertised! ;-)

    Ta Ta.

  • thakvika Apr 26, 2017 @ 1:50

    How can i just click my app and run rather than goto command and type ./myapp?

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.