Write, Compile and Run a C program on Ubuntu Linux : For Beginners
This tutorial explains how to compile and run your first C program on
Ubuntu Linux operating system.
As a prerequisite i assume that you have
1. Installed an Ubuntu Linux operating system on your computer (probably Ubuntu 10.10 or greater). The pictures shown below are from Ubuntu 12.04. The user interface would be a bit different for the older Ubuntu systems (older than 10.10)
2. A basic idea of Operating system(OS) & Files
3. A very basic idea of C programming OR maybe you could start learning after this session
Note:
1. This tutorial does not require installation of any additional programs.
2. Key words are emphasized, you could explore on them further in the Internet.
So here we go ...
STEP 1:
Click on the Dash home to your top left and type gedit in the text box. Gedit is a simple text editor. Click on the "Text Editor" icon.
STEP 2:
The text editor would open with an Untitled document. We would type our first C program in it.
I would not go into much detail about C programming. Just a little description of what the above program contains.
The first line of the above program tells the compiler to include a header file that contains function prototypes or function signatures to gain access to standard input output functions, hence the name stdio.h.
The main function is the point of start of execution of a C program. Here we call the printf function, by passing a string within double quotes "" that we want to appear on our terminal. The character '\n' represents printing of a new line after the string. And the semicolon ';' ends the statement.
STEP 3:
We would like to save our C program to hard disk. A simple way to do it is to press 'Ctrl' key and 'S' key on your keyboard together. A dialog box would appear that would ask you details about the filename and its location for saving the text file.
We name it as helloworld.c and save it in Desktop as shown below.
You can notice that once you save it, the text is highlighted! . Yes the gedit text editor has done syntax highlighting of our C program in the text file by understanding that the filename has an extension of ".c".
STEP 4:
Close the text editor. Now again click on the Dash home to the top left and type terminal in the text box. Click on the "Terminal" icon that appears.
Terminal is a command line application (CLI) that helps the user to interact with OS or run certain programs by issuing text commands at the command prompt provided by the terminal.
STEP 5:
Once the terminal is open, we would like to know where is our current location in the hard disk. So type the commandpwd and press enter (pwd represents present working directory).
In Linux '/' is used to separate directories or folders (a collection of files and other folders). The first '/' represents theRoot directory (A directory that contains everything that belongs to your operating system & applications).
The path shown is /tmp/guest-rJhn7b. Usually the path would be something like /home/<user name>, but since i have logged in as a guest user hence this path.
By default the terminal opens always at the
home directory of a user.
STEP 6:
Now we would like to know what are the contents of our current location. We type the command ls (list directory contents) and press enter.
You can see subdirectories like Desktop, Downloads, Music, Pictures etc.
STEP 7:
As we have saved our C program in Desktop, we would like to navigate to Desktop subdirectory. For that we would type the command cd Desktop and press enter. (Note: The directory and file names are case sensitive).
Again execute ls command to know contents of Desktop.
You could see the C program that we saved i.e helloworld.c
STEP 8:
Now we would
compile this program to create a
binary executable that can be run on your computer. We would use the
gcc (GNU C compiler) to compile our program.
Type
gcc helloworld.c and press enter. Gcc would exit without any errors or warning indicating that the compilation was successful.
Now to run the program type
./a.out and press enter and there you go with the output of your first program !!!
If an output filename is not specified the binary executable is always named a.out and placed in the same directory where you compiled your program.
An alternate way to compile the program is as shown below
gcc helloworld.c -o helloworld
this tells the compiler to compile the program helloworld.c and name the output binary executable as helloworld.
To execute the binary type ./helloworld (i.e execute a file helloworld which is under current directory. The current directory is represented by a dot '.'. And ./helloworld forms the path of the binary executable).
Hope you could now have fun compiling and running more programs on Linux.
And don't forget to comment in case you have any doubts. I hope i would be able to clear some basic doubts and for the rest of the doubts a lot of experts are out there on the Internet to help you :)