Compile and Run C on Ubuntu


To create a C program and run in on Ubuntu can be easy.

1. Install C and C++ Compilers in Ubuntu

Run the following command in the Ubuntu terminal, it will install all the required packages for C and C++ compilers on Ubuntu

sudo aptitude install build-essential

2. Write a Hello World program in C
sudo gedit hello_world.c

add the following lines save and exit the file

/* Program 1.1 Your Very First C Program - Displaying Hello World */
#include <stdio.h>   /* This is a preprocessor directive */
int main(void)   /* This identifies the function main() */
{   /* This marks the beginning of main() */
  printf("Hello world!");   /* This line displays a quotation */
  return 0;   /* This returns control to the operating system */
}   /* This marks the end of main() */

3. Create an Ubuntu executable using the following command

cc -o hello_world hello_world.c

4. run this executable using the following command

./hello_world

Output should show as follows

Hello, world!

Hope it helps! :)

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

  1. #1 by m0dk1d on October 4, 2008 - 11:13 am

    Another simple, straightforward and helpful article. Keep it up!

(will not be published)