C Language

To share this page click on the buttons below;

Compile the first program

Here we will have a look about the compilation of the first simple program we wrote in the previous chapter. Before I will also say something (very shortly) about tools used to program.

Programming tools

The minimum equipment you need to program in C is made by a good text editor and a compiler (of course I suppose you have a PC with an Operating System). On the other hand there are complete programming suites (which are commonly called Integrated Development Environment, shortly IDE) that group together all you can need to develop a C program. Here I am speaking about developing a C program that can be used on a PC, you can actually develop C programs for external devices and boards, in this case you will need some dedicated hardware to connect the board with the PC and download your executable on the board, but all this is outside of the scope of this chapter.

The list of all the available software that you can find to develop a C program is very long, here I will list only a couple of examples and only free options.

A good text editor available on Windows system is Notepad++ (which has a lot of plugins that can improve the basic features provide by this software). On Linux a valid free text editor is Geany or (if you like old fashion console based program) you can try something like Vim or Emacs (both are powerful, but really hard to use). Geany is also available on Windows. Another valuable text editor available both for Linux and Windows is Sublime text, which is not completely free: it has a license that you can buy, but you can use it also without a license if the pop-up Please buy a license does not bother you.

Talking about IDE Code::blocks is a good environment development available both on Windows and Linux systems. Searching on the web, you may find a lot of valid alternative, feel free to make experiments until you find the tools that fits your inclination or the project you are working on.

Personally I like a minimum set up with just a good editor and a compiler, because that gives me the feeling to control and understand all details of what is going on, but surely big integrated environment can speed up the development with fancy functions (auto completion code, integrated debugger and so on).

The compiler

The most widely used free compiler available is gcc, which stands for GNU Compiler Collection. gcc can be in fact used to build executable also for other languages different from C.

The gcc is available both for Linux and Windows. Instruction to install the gcc on a Linux system can be different depending on the Linux distribution you are using. On Ubuntu for example the gcc can be installed with the package build-essential.

sudo apt-get install build-essential

On Windows you can install gcc by installing MinGW which is the Windows porting of gcc. You can go to the official site of MinGW by clicking the link MinGW here you will find detailed information about download and installation.

The important point to follow the next steps is that the gcc must be available from the windows command prompt, i.e. must be in the path of Windows.

Assuming the reader is an absolute beginner I feel I must give some explanation about the last sentence. If you have already understood feel free to jump to the next paragraph.

The PATH is an environment variable (a variable defined at the Operating System level) that lists the folders in which the operating system look for an executable (the concept is common both in Windows and Linux). When you start an application you run an executable file which is stored somewhere in your PC. Usually you do not know where this file is and in most cases you even do not care. Once you installed the application the installer of that application performed a set of operations to inform the operating system where the executable is placed and set up an access to that executable via the menu launcher or some desktop shortcut. That is the usual way (you click on the icon), but here we are dealing with programming and so we need to have some more control.

Another way to launch an application is via the Command prompt (for Windows users) or the Terminal (for Linux users). The Command prompt on Windows can be started by typing on the Windows menu cmd.exe, on Linux look for the Terminal application.

The command prompt and the terminal allow the user to give commands to operating system and to launch applications. You can launch an application by typing the name of the executable file which correspond to that application. So suppose that you want to launch an editor and that the editor related executable is editor.exe which is placed in the folder C:\Program Files (x86)\myeditor. You can launch the editor by starting the command prompt and typing C:\Program Files (x86)\myeditor\editor.exe (and press return). You are giving to the operating system the absolute path of the executable. That can be annoying because you have to type a lot and you have to remember where your executable is placed. Alternatively you can type only editor.exe (and press return) but this second method is not guarantee to work. The operating system may complain that he does not know the command editor.exe. It works if the folder C:\Program Files (x86)\myeditor\ is in the PATH of the operating system because the operating system looks in all the folders listed in the PATH (which is simple a list of folders) for an executable called editor.exe.

Since we are going to launch the gcc from the command prompt or the terminal we need to have the path of the gcc.exe file in the PATH of the Operating System.

On Linux systems this is usually true so I will not deal with that. On Windows that can be false. First you have to locate the folder which contains the gcc executable: this is usually placed in the sub-folder bin of the MinGW installation folder you provided (or accepted) during the installation of the MinGW.

Then you need to go to the Windows's Control Panel → System → Advanced system settings → Advanced tab → Environment variables (pretty hidden, isn't it?).

You will finally come across to something like that

PATH settings on Window operating system

Here you find your PATH variables: one is valid just for you (the top one) and the other is valid for you and for everyone else will use the PC. By adding a folder in these lists (they are more precisely environment variables) you add this folder in the list of folders the Operating System searches when it is asked to launch an application.

Why do you have to take into account such details? Well first because we want to use gcc via command line and we do not want to type always the absolute path of the gcc executable, but also because we want to know which gcc version we are using (and we control that by putting into the path only the one of the version we want to use). It may happen in fact that you need to use different versions of compiler and you need to be aware of which one you are using for each project.

Compile the first program

All the previous is just to finally get there: to compile our first program.

Open the command prompt (Windows) or the terminal (Linux) and type gcc -v. If the Operating System complains that he does not know what gcc is, you have to set the path as explained before. If the command succeed the version of the gcc installed (or seen because of the path) will be printed on the screen.

Now the true compilation command. I suppose that you move the current directory of the command prompt into the folder that contains the source file and I suppose that the source file name is first.c. You change directory in the prompt or terminal using the cd command (but I will not deal with that).

Note that you can download the source file of the first program from there, although I suggest you actually type the file in you text editor.

Now type

gcc -o first.exe first.c

and then hit return.

This will invoke the gcc and it will ask it to take the file first.c and to turn it into an executable called first.exe. The name of the executable is set by the option -o: in other words the name written after the flag -o will be used to name the final executable.

If all goes well (if there are not error in your source file) you will find an executable file named first.exe in your directory. Now you can launch your first program by typing first.exe. Here it comes again the concept of PATH: on Windows system usually the current directory is considered to be in the PATH, so if you type first.exe on Windows the program will start, on Linux however by default the current directory is NOT in the PATH (of course you can change that by adding . (dot) to the PATH, . is the current directory and .. is the up directory), so you have to type ./first.exe (specifying that the program is in the current directory).

You should obtain something like that

Compilation of a simple C program output

If something is wrong in your source file the gcc will show you some strange error messages and you will need to fix all those errors if you want to get your final executable. Understanding the real meaning of an error message might be tricky: only practice will help you to understand.

Please note that gcc is a very complex program and it has probably thousands of different options: here I showed just the very basic usage. We will see something more when needed later on.

To share this page click on the buttons below;