Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Setup
Text Editor
Compiler
The compiler is a program that transforms the C source code into a program your computer can run. The
installation process for these is different depending on what operating system you are running.
Compiling and running C programs is also going to require really basic usage of the command line. This I
will not cover, so I am going to assume you have at least some familiarity with using the command line. If
you are are worried about this then search for online tutorials on using it, relevant to your operating system.
On Linux you can install a compiler by downloading some packages. If you are running Ubuntu or Debian
you can install everything you need with the following command sudo apt-get install build-essential . If
you are running Fedora or a similar Linux variant you can use this command
su -c "yum groupinstall development-tools" .
On Mac you can install a compiler by downloading and installing the latest version of XCode from Apple. If
you are unsure of how to do this you can search online for "installing xcode" and follow any advice shown.
You will then need to install the Command Line Tools. On Mac OS X 10.9 this can be done by running the
command xcode-select --install from the command line. On versions of Mac OS X prior to 10.9 this can
be done by going to XCode Preferences, Downloads, and selecting Command Line Tools for Installation.
On Windows you can install a compiler by downloading and installing MinGW. If you use the installer at
some point it may present you with a list of possible packages. Make sure you pick at least mingw32-base
and msys-base . Once installed you need to add the compiler and other programs to your system PATH
variable. To do this follow these instructions appending the value ;C:\MinGW\bin to the variable called
PATH . You can create this variable if it doesn't exist. You may need to restart cmd.exe for the changes to
take effect. This will allow you to run a compiler from the command line cmd.exe . It will also install other
programs which make cmd.exe act like a Unix command line.
cc --version
If you get some information about the compiler version echoed back then it should be installed correctly. You
are ready to go! If you get any sort of error message about an unrecognised or not found command, then it is
not ready. You may need to restart the command line or your computer for changes to take effect.
Hello World
Now that your environment is set up, start by opening your text editor and inputting the following program.
Create a directory where you are going to put your work for this book, and save this file as hello_world.c .
This is your first C program!
#include <stdio.h>
This may initially make very little sense. I'll try to explain it step by step.
In the first line we include what is called a header. This statement allows us to use the functions from
stdio.h , the standard input and output library which comes included with C. One of the functions from this
library is the puts function you see later on in the program.
Next we declare a function called main . This function is declared to output an int , and take as input an
int called argc and a char** called argv . All C programs must contain this function. All programs start
running from this function.
Inside main the puts function is called with the argument "Hello, world!" . This outputs the message
Hello, world! to the command line. The function puts is short for put string. The second statement inside
the function is return 0; . This tells the main function to finish and return 0 . When a C program returns
0 this indicates there have been no errors running the program.
Compilation
Before we can run this program we need to compile it. This will produce the actual executable we can run
on our computer. Open up the command line and browse to the directory that hello_world.c is saved in.
You can then compile your program using the following command.
This compiles the code in hello_world.c , reporting any warnings, and outputs the program to a new file
called hello_world . We use the -std=c99 flag to tell the compiler which version or standard of C we are
programming with. This lets the compiler ensure our code is standardised, so that people with different
operating systems or compilers will be able to use our code.
If successful you should see the output file in the current directory. This can be run by typing ./hello_world
(or just hello_world on Windows). If everything is correct you should see a friendly Hello, world!
message appear.
Errors
If there are some problems with your C program the compilation process may fail. These issues can range
from simple syntax errors, to other complicated problems that are harder to understand.
Sometimes the error message from the compiler will make sense, but if you are having trouble
understanding it try searching online for it. You should see if you can find a concise explanation of what it
means, and work out how to correct it. Remember this: there are many people before you who have
struggled with exactly the same problems.
For people feeling more confident a program called gdb can be used to debug your C programs. This can
be difficult and complicated to use, but it is also very powerful and can give you extremely valuable
information and what went wrong and where. Information on how to use gdb can be found online.
On Mac the most recent versions of OS X don't come with gdb . Instead you can use lldb which does
largely the same job.
On Linux or Mac valgrind can be used to aid the debugging of memory leaks and other more nasty errors.
Valgrind is a tool that can save you hours, or even days, of debugging. It does not take much to get proficient
at it, so investigating it is highly recommended. Information on how to use it can be found online.
Documentation
Through this book you may come across a function in some example code that you don't recognise. You
might wonder what it does. In this case you will want to look at the online documentation of the standard
library. This will explain all the functions included in the standard library, what they do, and how to use them.
Reference
hello_world.c
Bonus Marks
› Change the Hello World! greeting given by your program to something different.
Navigation
‹ Introduction • Contents • Basics ›