C Programming/Data types

From Wikiversity
Jump to navigation Jump to search

Lesson objectives:[edit | edit source]

  • Learn about some of the primitive (ie. builtin) data types.
  • Learn about integral and floating-point types.
  • Learn about how different types are stored in memory.
  • Learn about FPU.

Lesson[edit | edit source]

What are data types?[edit | edit source]

Data types are the way in which memory is abstracted away from the programmer. It allows us to store memory in a particular way so that code can be portable, and save us from constantly worrying about data sizes. For example, if data were just stored as a number of bits, what happens if you move to an (excedingly rare) 9-bit-byte machine? Or one where by default chars are 16-bit? Data types stop a programmer having to be concerned about these things.

What comes below the basic data types?[edit | edit source]

Each of the primitive data types learned here are one of two types: integral or floating point. Integral types store a literal integer in RAM - the sequence 1001 0011 1101 0111 in RAM represents exactly that number, in base-2, for the purpose of math performed upon it. Any int type (eg. int, long int, uint, int8_t, uint8_t etc.) or char type (eg. char, signed char, unsigned char) is an integral type. The reason a char works this way is because, when printed, the machine writes the values to memory containing the file (if you use a unix-like OS. The file is stored in RAM). Then when displaying this, the GPU displays in the correct position the pixels for each character in memory that is visible through the window. Note that a character can be by default either signed or unsigned - it doesn't technically matter, since it should only be used for characters, unless you want to use the int8_t/uint8_t,which are defined as signed char/unsigned char (if you use an 8-bit-char CPU). Note that these types can only store whole numbers - by which it is meant their memory, when used in any sort of math, as treated as representing a whole number, by which in turn it is meant that math the CPU does using only these types produces an output as if they were treated as being done with the numbers an base-2 integers. If you use an integral and a floating-point type, then the integral type is first converted into its representation as a floating-point number. The floating-point type in C is represented by all float and double types. The same keywords are available for integral and floating-point types, excluding the signedness factor, due to the way the number is stored in memory. Generally, a float is stored, for an n-bit number, as follows: 1st bit: represents the signedness, can be viewed as a plus-or-minus. This method of representation is why all floating-point types MUST be signed. 2nd to xth bits: represents the exponent. The radix to which the exponent is taken is system-defined. xth to nth bits: represents the significand. All floating-point math (should) take in only floating-point types, and output a floating point type. All floating-point math is done in a special unit of the CPU, called the FPU.

What is the FPU?[edit | edit source]

The FPU (Floating-Point Unit) is a unit within the CPU set up to perform math in floating point numbers. The reason this is done is, due to the different representations in memory, integral and floating-point numbers need a different physical operation to do maths on them. An FPU should be able to addition, subtraction, multiplication and division (but not a modulo, due to problems with accuracy), among other operations, within a very small error margin.

C basic data types[edit | edit source]

C defines 4 basic data types:

int

char

float

double

Note there is also the void type, which has 0 size and cannot be used as a type. The only time it's used is when doing pointer-type conversions. Note that, confusingly, when a void-pointer is accessed (ie. the value which should be void), a single byte is accessed (if possible). The main reason for not causing an error is probably that a void pointer is used for functions to move data, and constant conversions being written in the code, while not slowing it down, can obfuscate it, plus the void-pointer already stores an actual memory address to access.