C++ Tutorial

Variable Types



    There are many variable types, and as you will see, there are even types of variables that you can define to specifically deal with whatever purpose you have in mind for them.  The most important ones, and the ones that every other type is essentially derived from are the pre-defined types in C++.  Each of these types has an explicit and known memory storage configuration, and each of them are different.
    The first ones are the basic types, and are stored essentially exactly as one would expect them to be stored in the computer.  These are consecutive binary bytes of memory space.  The numbers are represented in simple binary notation (all 0's and 1's).  They are a char, short, int, and long.  These things are sometimes confusing and so we will go over each with due consideration.
    The 'char' data type is defined to be one byte.  This is always the same, and therefore this is the most reliable thing that one can deal with.  No matter what machine one is on, they are always stored as one byte (8 binary digits, or bits).  Given this representation, they may take on the values of -127 to 127, if they are 'signed' (i.e. have a bit reserved to indicate whether they are negative numbers or positive numbers), or 0 to 255 if they are unsigned (i.e. they do not have a bit reserved to indicate whether they are negative or positive).  These types are generally considered to be the building block of strings in C++, each value that the char can take on defined to be a specific ASCII (the almost universal representation for alphanumeric characters) value.  ASCII stands for the American Standard Code for Information Interchange.  So, when you try and output a char using an output routine, what will most often happen is that it will be output as a character ('a'-'z', '0'-'9', 'A'-'Z', or some other symbol).  Even though this is the case, all arithmetic operations work on them as though they were normal numbers.
    The 'short' data type is defined to be at least 2 bytes (16 bits), though they are also defined to be one half the size of an int, so if the int data type is larger than 4 bytes, then these too will be larger.  Given the 2 byte representation, they may take the values of -32767 to 32767, if signed, or 0 to 65535, if unsigned.  These have no special significance.
    The 'int' data type is defined to be at least 2 bytes, and generally the unit of addressing on the machine, the most common representation for these are 4 bytes, this needs explanation though.  The standard definition for ints are that they should be at least two bytes, at least as large as 'short's, but no larger than 'long's.  The more understandable definition is that they should be the standard unit of addressing on the machine.  This is because 'beneath the hood', so to speak, 'int's are the unit of pointers on the machine (addresses in memory).  By this token, most machines use 4 byte ints in the current architectures.  Given this, 'int's have values from -2147483647 to 2147483647, if signed, and values from 0 to 4294967295, if unsigned.
    The 'long' data type is defined to be at least the size of an int, with no appreciable limit, other than the limit of the machine addressing.  These are most often 4 bytes, even on 64 bit (8 byte) machines.

   The next native types are more complex types and are stored in the computer such that they are not immediately readable. These are float and double.

     floats are variables that are the same size as an int, by the ANSI standard, but are read as floating point numbers. These often are, just as ints are most often, 4 bytes, and therefore range from the values 1.2e-38 to 3.4e38.

     doubles are variables that are sometimes stored as one single large floating point value (if your machine has 64-bit floating point registers), or as two integer registers (if you only have 32-bit floating point registers). In either case, a double is defined to be twice the size of an int. In most cases, as stated above, this means that a double will be 8 bytes, and therefore, because doubles are read as floating point numbers, may hold values from 2.2e-308 to 1.8e308.