A bit of C - a quick guide


C is the grandaddy of many modern languages. It's influence can be seen in Java, PHP and, of course, C# and C++.

If you can write large and complex programs in C then you are a serious programmer.

Not that C is a large or complex language but it does have a reputation for being difficult to use. However, if you understand the language well, it is really not so bad. The philosophy behind C is to trust the programmer - if the programmer knows what she is doing, all will be well.

And while C is an old language, it is still widely used, particularly in situations where a program needs to be compact and fast. and it remains a very popular language.

This tutorial is going to give you a quick tour of C basics. By the time you finished you will have enough knowledge to write and run programs that do something more than just say 'hello'. There are two parts, the first covers:
  • the basic structure of a C program
  • how to compile and run
the second part will cover:
  • variables
  • operators
  • loops
  • decisions
  • functions

Advertisement


If you are using Raspbian, or indeed, just about any flavour of Linux, then C is very possibly built-in to your system. You can check by opening a terminal window and typing
gcc --version
and if it comes up with something sensible then you have it. If not, whil you have the window open, type
sudo apt-get install gcc
and wait a few minutes while it is installed.
Alternatively run the following
sudo apt-get install build-essential 
This will install the compiler as well as any other required libraries, etc.

Now that's sorted you can start writing programs.

You can use whatever editor you like to create your programs, although I tend to use Geany because it is lightweight, simple to use, runs on anything and supports a range of languages. If you don't find it in your programming menu you can type
sudo apt-get install geany
in a terminal window. Geany has a menu for building and running programs but if you are using a simple editor you will need to use the command line to run your program.

Your first program

Here is what has become the compulsory 'Hello world' program.

#include<stdio.h>

int main(void){
    printf("Hello world\n");
}

C is a compiled language. This is a different way of doing things to, say, Python, PHP or Javascript. These languages are run from their source code and so you only see any syntax errors when the code is actually run. A compiled language, like C, is translated into machine code before it can be run (machine code being the very low level code that your computer actually understands).

With a compiled program, the syntax errors are found during the compilation process, so there won't be any these types of unwanted errors lurking in bits of code that have not yet been run. Compiled programs also tend to run faster.

So a C program is compiled first. But their is another stage before we can produce a runnable program. That stage is called linking. C libraries are provided for various uses, for example, maths or input/output, and after your program is compiled, it is linked with the required libraries to produce the final program.

None of this is anything to get too alarmed about as it is pretty much automatic but it is as well to know what is going on under the hood because you may need to know about it if you decide to use C in anger.

Enough of that, let's run the program!

Save your program as hello.c. And if you are using Geany, from the Build menu first select 'build' and then 'run' or 'execute' ('build does the compiling and linking, while 'run' does... well the running, obviously).

To run the program from the command line, navigate to the folder where the hello.c is and type
gcc hello.c
Now take a look at the files in the folder (use the command ls in the terminal window), You should see a new one called a.out. This is the runnable program that the compiler has produced, so type
./a.out
and you will be greeted with "Hello World". (If you have not seen the './' in front of a file name before, that's just the way to run an executable file from the current folder.)

So, a.out is the default name for the runnable program but we'd rather choose our own name, I think. Try this
gcc hello.c -o hello
Now look and you will see that you have a file called 'hello'. Type
./hello
and you will get the same result as last time (you can delete a.out, now). The '-o' is called a switch and it tells the compiler to give the output file a name.

Geany users will find all this is done for them and there is no need to use a terminal window, at all. However, to compile and run your program from outside of Geany, use the steps above.

Simple as it is, I suppose we'd better see how this program works.

Advertisement


The anatomy of a C program

Let's look at each line - it won't take long.

The first thing you see is
#include<stdio.h>
this is how we include libraries in C. Libraries contain all manner of useful functions for maths, networking and lots of other stuff. But in this case, the library provides the standard input/functions that let us read and write to the terminal and to files.

The library is called stdio.h and this is included in chevrons (<>). The use of the chevrons means that the library is available within the operating system (in whatever directory that happens to be). If you write your own libraries then their name would be enclosed in quotes.

The #include part of the line is a directive to the compiler to incorporate the stdio library. There are other compiler directives and they all are preceded with a #.

Now we get to the program proper. All C programs start at the beginning of a function called main. It doesn't matter where in the program this function is, this is where it starts. So the next things we see is the definition of this function.
int main(void) {...}
A C function declaration comprise of a return type, a name, in braces a set of parameters and then the body of the function enclosed in curly braces. In this case the return type of the function is int, meaning an integer is returned from the function. Return types can generally be any valid data type but for the main function it is always an integer - this value is returned to the operating system and it signals whether the program completed successfully or not.

Next we have the name of the function - main - which is followed by the parameters that are passed to the function. void means that there are no parameters. void is one of the two valid values for parameters in the main function. We'll see the second one later.

The next thing are a pair of curly braces {}. These enclose the code that implements the function.

And the code in this program is simply
printf("Hello world\n");
printf is a function from stdio and simply prints something to the screen. In this case a string of characters.

If you are a Python programmer  you might be interested to know that, although C programs tend to be nicely laid out with code indentation, this layout is entirely up to the programmer. C doesn't enforce indentation like Python because it uses the semicolon (;) to indicate the end of a line of code and curly ({}) braces to delimit a block of code.

That's your program then. Just a couple of things to point out.

The \n in the string represents a new line character and I said that the main function returns an integer, so where is it?  This is perhaps cheating a little but if you don't specify a return value in main, the value 0 is returned (which means that everything ran successfully).


Advertisement


Let's get a bit more complicated

I am assuming that you have done at least a little programming before, so that you will know what a variable is, what loops and if statements are for, and how a function is used.

If you looked at my blog posts about languages for the Raspberry Pi you will be familiar with this program. It is a simple guessing game but it serves to show many of the fundamental features of C.

It's not very long; here it is:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// define a function to get a guess
int guess(void){
    printf("Enter a guess (1 to 10): ");
    int a;
    scanf("%d",&a);
    return a;
}

// Start here

int main(int argc, char **argv)
{
    time_t t;
    srand((unsigned) time(&t));
    int x = rand()%10+1;

    for(int i = 0; i < 5; i++){
        int answer = guess();

        if (answer == x){
            printf("Correct!\n");
            break;
       }else{
            printf("Wrong!\n");
       }
    }
    printf("The answer was %d\n",x);
}

It's a simple guessing game. When you run the program, a number between 1 and 10 is randomly selected and the player is asked to enter an integer between 1 and 10 (a guess). The number entered is checked against the randomly selected one and if the guess is correct, an appropriate congratulatory message is displayed and the program ends. If not the player is given four more attempts to get it right. If you cut and paste this program (or type it in - carefully) and save it as guess.c, you can can compile and run it in the same way as above but substituting guess for hello.

It's a simple program but it illustrates the use of variables, loops, decisions and functions. What more could you ask for? (Actually, you could ask for an program that uses pointers but that will have to wait - let's learn to walk before we start running.)

You can see that the layout is the same as the 'Hello world' program, except that we define two functions, main and another called guess. And, also, we see the second version of parameters for the main function. They aren't necessary for this program and I only put them here for illustrative purposes. They look like this:
int argc, char **argv
There are two an integer that tells us how many parameters there are and a pointer to a list of strings (which are the actual parameters). So if you were to run the program like this;
./guess p1 p2
the parameter argc would have the value 3, this being the number of strings on the command line (including the name of the program) and argv would point to the values of those 3 strings. But enough of that! Let's get on with the program.

Apart from the fact that there are two functions, the layout is the same as before. First we include the libraries. There are two more of these, stdlib and time. These contain the random and time functions that we will use.

Another new thing that we see is comments. A comment is for human use only, it is ignored by the compiler. We see comments here that begin with //. These are single line comments. One of the comments says
// Start here
So we will. This is the definition of the main function where the program starts. It looks similar to before but the first thing we encounter inside of the main function is a variable declaration.

Variables

In C variables must be declared before they are used and this declaration must specify the type of the variable. This is done so the compiler can allocate storage for the values of those variables. So, if we wanted to use a variable called x and that variable was to used for integer numbers we would write the line
int x;
and if we also wanted to give the variable an initial value, we could write something like this instead
int x = 0;
Variables can have different types, as you might expect, there are different types of integers, real numbers (floats) and characters. There are no string types in C. We will not go into the representation of strings in C, here.

Again, if you are used to a language like Python, Javascript or PHP, this all may seem a little odd but it is essential that variables are declared with their name and type before use; your program will not compile otherwise.

Now be warned, we are not going to go into tremendous detail in describing our guessing game program because otherwise this tutorial would turn into a small book.

The first variable that we see in main is called t and is of the type time_t. Now time_t is not a type that you may have come across before. This is because, in C, we can invent new types and this one is from the time library and it is used to represent a time (what else?).

So no great detail here but our aim here is to generate a pseudo random number within the range 1 to 10. The rand library gives us a function that will give us a random number but, unless we seed it, it will give us the same sequence of numbers each time we run the program. Seeding the random number generator will make it produce a certain sequence of random numbers, so seeding it with the time (which is, of course,different each time we run the program) will ensure we get a different sequence of numbers each time. This is what
srand((unsigned) time(&t));
does. (I'm not going to go into the precise operation of this bit of code as it requires an understanding of pointers - for the moment just use it as a piece of boilerplate code.)

Next we use the function rand() to get us an actual number. This number will be in the range 0 to 32767 (at least). We reduce this to a range of 0 to 9 by dividing by 10 and taking the remainder - this is what the % operator does. Then to get a number from 1 to 10, we simply add 1. Here's the line of code that does all of this
int x = rand()%10+1;
Oh, hang on... operators, I guess we need to do a quick excursion and quickly look at them.

Advertisement


Arithmetic operators

As you might expect C lets you do basic arithmetical operations, so adding subtracting, multiplication and division are all there. But there are a few other operations that can come in handy, too. Here's a (not comprehensive) list with examples:

a = 1  + 2;
b = 2 - 1;
c = 2 * 10;
d = 10/2;
e = 11%2;
a++;
b--;

Let's assume that all the above variables are integers. The first four operations are obvious, the only thing to say is that dividing integers will lose any decimal part. The fifth operator is the remainder operator; it performs an integer division and them gives you the remainder. The final two operations are increment and decrement, i.e. add 1 and subtract 1. These are quite convenient as we shall see soon.
There is more to learn about operators in C but that will suffice for the moment.

The for loop

Loops repeat a block of code a number of times according to certain conditions and, in C, a block of code is enclosed in curly braces. In our program we want to ask for a guess five times but stop if the correct answer is given. To do this we use a for loop. It works like this:
for(int i = 0; i < 5; i++){...}
First the curly braces contain the block of code that is going to be repeated. The loop is defined by three expressions in braces following the word for and separated by semicolons.
The first expression is executed only once, before the loop begins. In this case it declares a variable called i and initialises it to the value 0. This might seem an odd place to declare a variable but because it's done as part of the loop, it is a local variable and only exists inside this for loop. This is useful as it cannot be confused with any other variable called i that has been declared outside of the loop.

i is going to be used to count the number of time the loop executes.

The second expression is the condition that stops the loop or tells it to carry on. Here it is i<5, meaning that while the variable i is less than the value 5 the loop continues.
The final expression is i++ which adds one to the variable. This is done at the end of each repetition. In other words it as if this was the last bit of code inside the block.

So, when the loop begins, i is set to zero at the end of each repetition i is incremented by 1 and the loop continues while the value of i is less than 5. This means that the block of code is run when the value of i is 0,1,2,3 and 4 - when it gets to 5 the loop stops. So the loop is executed 5 times.

So what is inside the loop? Basically, we want to ask the player for a guess and then check whether it is correct or not.

We get  a guess by invoking the function guess() and assigning the value that is returned from that function to a local variable answer. That's done like this
int answer = guess();
To call a function we just write its name followed by a pair of braces (if there were any parameters to pass to the function, they would go here) and we get the value of the function with a simple assignment statement.

We'll look into the function itself later.

We check the value of the guess with an if statement. They look like this
if (condition){...}
else{...);
although the else part is optional.

The condition is usually a test of equality or other relationship, (less than, greater than, etc.). Here we look to see if the value of the guess (in answer) is equal to the randomly selected number in x.
If the test is true then we execute the block of code after the if, otherwise we execute the block of code after the else. And we simply print a suitable message for each possible result.

Except, of course, we want to stop the program if the answer is correct. This is what the break statement does. break stops the loop execution and jumps to the point in the program immediately after the for loop (which is just a print statement confirming the value that was guessed).

That only leaves the function to be dealt with.

Functions

The function here looks like this
int guess(void){
    ...
  return a;
}
It returns an integer value which is why the name is preceded with int, it has no parameters, so we have void in the braces following the name. Finally, the last statement in the block of code that comprises the function is a return statement. This specifies the value that will be given to the calling function. The rest of it you have come across before, except scanf. We need to know a bit about pointers to explain this properly, so suffice it to say that this gets an integer value from the keyboard and puts in in the variable a.

And that's it!

Advertisement


So what have we missed?

Lots!

There are other data types to talk about - we've only dealt with integers, here. And then there are structures, too.
More loops and the switch statement.
Pointers! C programming invariably requires the use of pointers and we have glossed over their use here. You really do need to have a good grasp of the use of pointers to be able to call yourself a good C programmer.
 We have also only briefly talked about printf and scanf, there is a bit more to them than I've explained here and then there is also their cousins fprintf and fscanf for file operations.
OK, so there's a fair amount missing. But it takes a book to teach C in its entirety (see below).



C Resources

See below reviews of a couple of books on C programming.
Learn to Code with C
The C Programming Language


Advertisement


Comments

Popular posts from this blog

Thonny, Idle or Geany?

Just Enough Python

Introduction to Python by Andrew Ng