ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Compiling and running C programs in Linux

Updated on April 12, 2011

When you write C programs, you'd use a text editor and save them into files. These files are known as "source code". Just like any other text files, these files can be easily read, shared, compressed, copied and modified.

One thing you can't do with your C source code, however, is execute it. In other words, after you write and save your C program source code, you can't run the program and use it. You will need to convert this text source code into a file that the computer can execute. This is achieved in two steps known as "compilation" and "linking".

C compiler and linker

The C programming language is carefully designed to facilitate the creation of compilers to be easy and straightforward. Thus, today, there are quite a number of C compilers that you can choose from which range in features, stability and price. Among the most popular ones is the free and open-source compiler called "GCC" or GNU Compiler Collection (See

GCC is really a collection of compiler programs to compile code written in various languages like C, C++ and Java amongst others.

For compiling C programs, we use a program aptly called "gcc". To locate this program in your Linux box, please type the command below on the terminal:

which gcc

In my computer, I see the following results: (yours may vary)

$ which gcc

To check what version of gcc is installed, just type:

gcc --version

The linker used in Linux is called "ld" (that's the letters "el" and "dee") from a software package called binutils ("binary utilities"). The linker is called normally after the compilation process. It is responsible for combining multiple binary files called "objects" into a single "executable" file.

The executable file is the ultimate output of the compilation and linking processes. This represents your C program as a ready-to-run file. This is also now considered as a "binary" file since the contents of this file is no longer text. It can have characters which aren't even printable on the screen. This is the file you'll use to see your program in action. Once you have this executable file, you can then hide away your source code.

Standard C library

We're almost at the exciting part of actually writing a program, compile and link it and finally run it. However, I'd like to quickly discuss the so-called "standard C library" and then we'll be set.

The C programming language has been standardized by an international organization called "ISO" or International Organization of Standardization (See This promotes a consistent implementation of the language and allows interoperability between different systems.

Part of the ISO C standard is a specification of a set of functions that provide very basic but common operations like manipulation of character arrays ("strings") and handling input and output.

It is helpful, therefore, to familiarize yourself with the standard C library when writing your program otherwise you might end up writing a functionality that's already available in the library.

Linux already ships with the standard C library by default and can be found usually in the "/lib" folder. The file you'd look for is "". You'll notice that it will have a number in the filename and will differ from system to system, this number is just for versioning.

$ ls -l /lib/
/lib/ ->

Note in the output above, "" is a symbolic link to a file called "" where "2.9" is the specific library version.

In order to use the functions in the standard C library, you'll need to include in your programs the so-called "header" files. These files, usually with a file extension of ".h" (that's dot "h"), contain the function prototypes which basically specifies how to call them from your program. You can open up these header files with your text editor as they're really just simple text files.

Because the standard C library packs numerous functions, several header files are used in order to group related functions. It's important to include the correct header file or files of the functions you want to call from your own program.

Let's take for example a function that prints out text on to the screen called "printf". This function is defined in the header file called "stdio.h". Therefore we need to include "stdio.h" in our program in order to use "printf" like this:

#include <stdio.h>

int main(int argc, char **argv)
   printf("Hello there!\n");

Compile and run

I've mentioned the two steps "compilation" and "linking" above in order to create an executable version of your program. However, the linker "ld" can be very strict and may require specific command-line arguments and sometimes even the order of the arguments can upset it. Luckily, the "gcc" compiler can take care of calling "ld" for us. Thus, I would not attempt to discuss "ld" here, but rather just use "gcc" to make our lives simpler.

So fire up your favorite text editor and type the program below:

#include <stdio.h>

int main(int argc, char **argv)
   printf("Hello there!\n");
   return 0;

Save the program to a file with a ".c" file extension, I'll use "hello.c". Then we'll compile and link it using gcc like this:

gcc hello.c -o hello

Note that we're running the "gcc" program giving it our "hello.c" file for processing. Then we also have "-o hello" which tells gcc to write its output into a new file called "hello". If the "hello" file already exists, then it will be overwritten.

What gcc will do is read your C program line by line from top to bottom and translate your code into the so-called "machine" code. This machine code is what is written to the output file "hello". What you won't see is that it'll actually create some temporary files during the process of compilation. Then at the final stage, by default, gcc will invoke the linker in order to create the final executable file "hello".

You should now have a file called "hello" in the same directory as "hello.c". To execute your program, on the terminal you'd type:


The output of this simple program would simply be the words "Hello there!" printed on the screen. Below is the transcript of my terminal session when running our "hello" program.

$ ./hello
Hello there!


There you have it! If you were to modify your program, then you'll need to recompile it again in order for your changes to be translated on to the executable version. Try it, make it break and see what happens. The gcc compiler will print out some error or warning messages to you if it discovers some invalid statements or something that might not make sense.

Don't be afraid if you get an error or warning, it's one of the best ways to get experience in C, or any programming language for that matter. By running on to these error conditions, you expose yourself to what the compiler would say or how it behaves. So that when your program grows in complexity, you'd be better able to fix these problems when they arise.

So go on, try to remove those semi-colons and see what gcc would say. Or change printf into "print" or try and remove the "#include" line and see what gcc says. Or if you like, open up the "hello" file in your editor to see the contents; don't be surprised that it will look like just a bunch of useless characters to you. These would really really help you later on.

I will be covering a lot of the interesting topics about C programming, check out my intended coverage by clicking this link: C programming in Linux.

Congratulations, you have just become a C programmer!


    0 of 8192 characters used
    Post Comment

    • profile image


      3 years ago

      You probably could do a tutorial over makefiles! I heard they can make things easier.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)