Back to A Brief Guide to C

note: These are formatted as follows:
  • Datatype: Explanation

    Issues with use

Basic Datatypes

A note of explanation: These are almost certainly not all the datatypes available with your compiler. You probably have special ones that allow you to use very long integers, for instance. I will just be describing the ones that are completely standard, and they'll be what you use 99% of the time.
  • int: The integer.

    A whole number, whose size depends on the processor you are using. For instance my Pentium 2, a 32-bit processor, would hold 4,294,967,296 total numbers, half being negative, one being , and the rest being positive (in my case, this is a range between -2,147,483,648 and 2,147,4836,47). The number is held in 2's Complement format.

  • float: Floating-point number.

    A decimal number, 4 bytes long in memory, consisting of the following parts:

    • Sign Bit: One bit, denoting if the value is negative or not. If this has 1 it is negative.
    • Mantissa: 23 bits, holding a number which is >1 and <2. If the exponent is at the very bottom of its range, the most significant digit of the mantissa may become 0, enabling the float hold smaller values at the expense of accuracy.
    • Exponent: 8 bits, holding the exponent + 127 (the number is stored unsigned, so this "bias" is needed to extend the range below zero).

    Remember, kids, there's a reason why this type is nicknamed "liar." Never, ever use this type if you are concerned at all about accuracy. The only reason to use this over a double is if you don't care about getting the value (almost definitely) wrong and need the 4 byte difference between the types. This type has 6 or 7 significant digits; stores numbers between 1.175494351 E -38 and 3.402823466 E 38

  • double: Double-accuracy Floating Point.

    Another decimal storage type, except this one is useful when you actually care about getting the answer (almost definitely) correct. This type holds numbers in a range between 2.2250738585072014 E -308 1.7976931348623158 E 308. Has components like the float, with some modifications:

    • Sign Bit: Same as before
    • Mantissa: 52 bits, holding a number >1 and <2
    • Exponent: 11 bits, holding the exponent + 1023

  • char: Character

    One byte, holding a number which corresponds to a character. Check the Table of ASCII Characters for the conversion from number to character. Remember that this type can be very useful for small values, flags, etc.

Modifiers

  • unsigned: Force the type to only store numbers greater than 0.

    This is very good to use if you only need to store positive numbers, and would like to double your range (instead of a char storing -128 to 127, and unsigned char would hold 255 to 0)

  • long: Enlarge the type to hold more values at the expense of memory space

    This has non-standard effects, depending on your processor and compiler. Run the chunk of code at the end of this chapter to test the size of the resulting type.

  • short: Shrink the type's size to conserve memory space at the expense of the type's range of values

    Again, this has non-standard effects depending on your compiler and processor. Check the resulting size with the code at the end of the chapter.

Testing the Size of Types


/*test the size of your favorite types on the system of
  your choice. replace foo's type with what you want to 
  see the size of.*/

#include <stdio.h>

int main() {
    long int foo;
    unsigned char size;
    
    size = sizeof(foo);
    printf ("%d\n", (int)size);
}

Log in or registerto write something here or to contact authors.