Understanding the Limitations of C Numeric Data Types

Using the C int Data Type - Mark Alexander Bain
Using the C int Data Type - Mark Alexander Bain
C is a fast and powerful programming language. However, the unwary programmer may find their program produces some unexpected results. Find out why.

C may not be the newest programming language around but there are a number of advantages for anyone starting programming. C is:

  • very fast;
  • platform independent - the same code can be compiled on Windows computers, Linux PCs and Macs;
  • hardware independent - it can be used everywhere from a washing machine to a mobile phone;
  • code can easily be reused by making use of C functions.

However, every program will need to make use of variables (information stored temporarily in the computer's memory) and so it is important that the new programmer understands the limitations of the C data types.

C Data Types

C data types (or datatypes) defines the values that can be stored in memory for any value.These data types are divided into three groups:

  • numerical data types
  • character data type
  • user defined data types

However, it is worth noting at this point that C uses weakly typed data types. This means that the data type of a variable can be changed programmatically and is not necessarily fixed during a program run. This can be useful but can should be used with care.

Changing from one numerical data type to another may make handling calculations easier, but changing a character data type into a numerical data types could be catastrophic and cause the program to crash.

Numeric Data Types

There are three numeric data types used in C:

  • int - whole numbers storing up to 4 bytes of data;
  • float - floating point numbers storing up to 4 bytes of data;
  • double - floating point numbers storing up to 8 bytes of data.

The int data type can also to subdivided into:

  • short - limited to 1 byte of memory
  • standard - limited to 2 bytes of memory
  • long - limited to 4 bytes of memory

In addition the int can be:

  • signed (stores positive and negative numbers)
  • unsigned (stores only positive numbers)

These subdivision enable the programmer to optimize the memory usage of their programs, but can lead to some interesting (and disconcerting) effects.

Understanding the Memory Usage of C Numeric Data Types

A programmer may, for example, decide that they wish to use just 1 byte of memory for a new variable that will store an integer. In that case they could use a short int and the code would be:

short int numb;

Here a new variable (numb) has been defined, and so the next step is to carry out a calculation:

numb = 32767 + 1;

The logical assumption at this point is that the variable now contains the value 32768. However, that's incorrect. If the programmer examines the variable they will find that it actually contains the value -32768. This is because the short int (which is signed by default) is only 1 byte. It therefore can only store a range of numbers. This range is not infinite and is, as the experiment shows, from -32768 to 32767.

With this knowledge a new C programmer can go on to write efficient code that won't suddenly start producing any unexpected results.

Mark Alexander Bain, Mark Alexander Bain

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Helpful?
Advertisement
Advertisement