C PROGRAMMING

Data Types

Date Published:
Last Modified:

The Basic Data Types

charThe smallest type on an architecture. Usually 8-bits wide. Sign depends on implementation.
intThe only type that the modulus operation can be applied on. At least 16-bits in size. Usually 32-bits on 32-bit systems.
floatSingle precision floating point number. Typically 32-bits wide.
doubleDouble precision floating point number. Typically 64-bits wide, although can be analogous to float (i.e. 32-bits wide) on smaller systems such as 8-bit microcontrollers.
long doubleNot normally supported on embedded platforms. 128-bits.
voidSpecial case data-type.

Using sizeof()

sizeof() can be used to return the number of bytes each type uses.

“There was a young man named Wight, Who invented the thirteen bit byte. You’ll get so much more, from your memory, I’m sure. But sadly your sizeof ain’t right.”

Fixed-width Integral Types

The problem with using int and all of it's derivatives (short int, long int, long long int, e.t.c) is that the width of the integer is platform specific. It is normally the same width as the platforms bus, but at least 16-bits. It is also called the natural width. For example, on an 8-bit system, an int will be 16 bits wide (remember, the C standard specifies it can't be less than 16 bits). On a 16-bit platform, it will usually be 16 bits, 32 bits for a 32-bit platform, 64 bits for a 64-bit platform, and so on, you get the idea!

To write portable code, it is usually better to use fixed-width integral types.

Fixed-width integral types also need special symbols for printf() statements. These are specified in <cinttypes.h>. They begin with the letters PRI.

Floating Point Support

Most higher-end microcontrollers and CPUs will have a hardware floating-point unit (FPU) inside them, which allows the CPU to do fast floating-point arithmetic. If you have a lower-end, cheaper microcontroller, it may not contain a FPU. In this case, you really have two options if you want to manipulate numbers with decimal precision:


Like this page? Upvote with shurikens!

Tags:

    comments powered by Disqus