C variables and types

Submitted by Webbot on August 10, 2009 - 2:30pm.

When reading someone elses code you may find variables declared with names that are unfamiliar and which naming convention should I use?

 

C defines types such as 'int', 'signed int', 'unsigned short' etc. I always find these somewhat confusing? Why - well it's down to how the original specification of the language started. Originally an 'int' was the same size as the 'data bus' - ie for an 8 bit processor then an 'int' would be an 8 bit number but on a 32 processor it would be a 32 bit number.

 

For me - this is all too linked in with the hardware. As a programmer: if I want a variable to store a value in a given min/max range then I want this to work regardless of the hardware it is running on.

 

The standard WinAVR include file "stdint.h" that is installed as part of AVRStudio does this for you by defining some new types.These have the form 'uint8_t' or 'int16_t' where the first example can store an unsigned 8 bit number and the second example can store a signed 16 bit number. Why do I prefer these names - because they say how many bits they use and whether they are signed number (eg +ve and -ve) or just unsigned numbers (+ve only).

 

I would encourage you to use this nomenclature in your own code as it will make your code much more portable between different hardware.

 

So here is a simple table to help you decide what kind of variable to use depending on the values that you want it to contain:-

Min Value Max Value Type
0 +255 uint8_t
-128 +127 int8_t
0 +65,535 uint16_t
-32,678 +32,767 int16_t
0 +4,294,967,295 uint32_t
-2,147,483,648 +2,147,483,647 int32_t

When deciding the best variable to use then start at the top and find the first entry which encloses both your required minimum and maximum value.


So let's assume you need a variable to store a number between -10,000 and +20,000 then the answer would be 'int16_t'. So we can declare our variable as:

int16_t myVariable;

 

Note that an 'int32_t' could also be used as it contains all of the required variables. This would work fine - but because it is a 32 bit variable, as opposed to a 16 bit variable' then it will take up twice as much space in your ram.

 

How can something define new variable types like this? It's done using the typedef command which allows you to create a new data type based on an existing one.

Can this help me when writing my own code? Yes. Imagine you need a data type to use to hold the speed of a motor where 0=stop, -ve numbers are reverse, and +ve numbers are forward. You may start by deciding that you want to express the speed of a motor as being a value between -100 and +100. So from the above table we could use an 'int8_t' ie

int8_t speed = 0;

 

However we may then create functions that need to specify a speed as one of the arguments such as a 'setSpeed(value)' which we could do like this:

// Variable holding the current motor speed

int8_t speed = 0;

// A function that stores a value into the variable

void setSpeed( int8_t newSpeed){

speed = newSpeed;

}

 

Of course, a big program, may have lots of references to a speed. So what happens if we change our mind and decide that a speed can be in the range

-10,000 to +10,000 and so we now need to use an int16_t. Suddenly it becomes a nightmare as we have to find all of the 'int8_t' in the code, decide if it is refering to a speed, and if so then change it to an 'int16_t'.  What a pain.

 

You can make your code much more readable and easier to change by creating a new data type for the number you want to hold. If we had done this from the beginning we would have written:

// Create a new datatype called MOTOR_SPEED that is the same as an int8_t

typedef int8_t MOTOR_SPEED;

// Variable holding the current motor speed

MOTOR_SPEED speed = 0;

// A function that stores a value into the variable

void setSpeed( MOTOR_SPEED newSpeed){

speed = newSpeed;

}

Notice how much easier the code is to read!  Also note that the compiled program will not be ANY bigger - ie this comes for free ! But even better - once we decide that a motor speed needs to become an int16_t then all we have to do is change that first line to:-

typedef int16_t MOTOR_SPEED;

And that is it. All done.

 


( categories: )