int
- The most basic and commonly used integral type is "int".
- The int data type is always the "best" size for the particular computer it is running on, typically 32 bits
- Format specifiers for ints are either %d or %i, for either printf or scanf.
long int
- A long int typically uses twice as many bits as a regular int, allowing it to hold much larger numbers.
- ( The C standard only specifies that a long cannot use a fewer number of bits than a regular int )
- printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int.
- long int may also be specified as just long.
long long int
- C99 introduces the long long int, typically having twice as many bits as a long int and printed/scanned using %lld or %lli format specifiers
short int
- A short int may use fewer bits than a regular int, thereby saving storage space.
- ( The C standard only specifies that a short int cannot use more bits than a regular int. On many machines short ints use the same number of bits as regular ints. )
- printf and scanf replace %d or %i with %hd or%hi to indicate the use of a short int.
- short int may also be specified as just short.
unsigned ints
- Unless otherwise specified, all of the aforementioned int types are signed numbers.
- Any of the above may be preceded by the keyword "unsigned" to indicate that they are unsigned.
- e.g. "unsigned int", "unsigned long int", "unsigned long", etc.
- "unsigned" by itself implies "unsigned int"
- The format specifier for unsigned integers in decimal form is %u. The u may be preceded by l, ll, or h for long, long long, and short unsigned types respectively.
- Unsigned integers can also be printed or scanned in octal or hexidecimal form using the %o, %x, or %X format specifiers in place of %u.
char
- Normally chars are interpreted as characters ( see below )
- Technically the char data type is an integral type, always having exactly 8 bits, ( known as a byte. )
- Signed chars can have values from -128 to +127, and can be printed as integers using %d or %i format specifiers
- chars can also be specified as unsigned, giving them a range from 0 to+255.
- Unsigned chars can be printed using %u, %o, %x, or %X format specifiers.
Integral Constants
- int constants in decimal form must begin with a digit from 1 to 9, and can be followed by additional digits from 0 to 9.
- in octal form they must begin with a 0, and following digits must be in the range from 0 to 7.
- in hexidecimal form they begin with 0x. Followng digits must be in the range 0 to 9, or A to F, or a to f.
- Any of the above may be preceded by a minus sign.
- int constants may be followed by a u, l, or ll to indicate unsigned, long, or long long constants respectively.
- Allowable formats are as follows, where the [ square brackets ] denote optional characters:
Decimal: [±]1-9[0-9...][Ll][Uu] Octal: [±]0[0-7...][Ll][Uu] Hexadecimal: [±]0x[0-9a-fA-F...][Ll][Uu]Integer Overflow
- When integer math yields a result that is too big ( or too negative ) to be represented by the corresponding integer type, then overflow ( or underflow ) is said to have occurred.
- With unsigned numbers the result is defined to "wrap around" to the other end of the integer's range, so if you add 1 to an integer that is at the maximum value for it's type, the result is zero.
- With signed numbers the result of overflow is undefined. In some cases adding 1 to the maximum positive value will wrap around to the most negative value, but in other cases erratic behaviour or even a program crash may occur.
- Floating point types include all types in which a number may have a fractional component. Fortunately there are only three that we need to worry about - float, double, and long double.
- Specific details of the floating point types available on a particular implementation, along with the number of bits allocated to each one and their minimum and maximum allowable values can be found in the file float.h
float
- The most basic type of floating point number is the float type.
- According to the IEEE standard, a single precision floating point number is exactly 32 bits long, comprised of:
- one bit for indicating the sign
- 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. This works out to about 6 or 7 decimal digits of precision.
- 8 bits for a binary exponent, to shift the digits left or right. This works out to an absolute range from about 10^-38 to 10^38
- Note that because a float only has 24 bits available for storing the digits of the number, it can actually be less precise than a 32-bit int for certain large integers.
double
- The double precision data type uses twice as many bits as a float, yielding approximately twice the number of digits of precision.
- According to the IEEE standard, a double precision floating point number is 64 bits long, comprised of:
- one sign bit.
- 52 bits ( plus one implied ) for recording digits, which works out to about 15 decimal digits of precision.
- 11 bits for the exponent, which works out to a range of about 10^-308 to 10^308
- The double data type is the preferred floating point type for most scientific and engineering calculations.
long double
- The long double type is guaranteed to have more bits than a double, but the exact number my vary from one hardware platform to another. The most typical implementations are either 80 or 128 bits.
- The IEEE standard for quadruple precision floating point numbers is 128 bits consisting of:
- one sign bit
- 112 bits ( plus one implied ) for digits, working out to about 34 decimal digits of precision
- 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932
Floating point constants
- Floating-point constants are normally indicated by the presence of a decimal point, and are normally doubles.
- Floating point constants may be followed by either an "F" to indicate an ordinary float, or an "L" to indicate a long double.
- Floating point constants can also be expressed in scientific notation
- Allowable formats are as follows:
[±]1-9[0-9...].[0-9...][Ee[±]0-9...][FfLl] [±][0].[0-9...][Ee[±]0-9...][FfLl] [±]1-9[0-9...]Ee[±]0-9...[FfLl] [±]1-9[0-9...]Ff[Ll]
Character Constants
- Character constants are enclosed in single quotes, such as 'A'.
- Character constants may also include escape characters such as '\n' or '\t', as shown here:
Escape Sequence Meaning
\aalarm ( bell ) \bBackspace \fForm feed ( clears screen ) \nNew line \rCarriage return \tHorizontal tab \vVertical tab \\Backslash \?Question mark \'Single quote \"Double quote \0Numerical zero ( null byte )
- The \ can also be used to escape a 3-digit octal numerical constant, or a hexidecimal constant beginning with \x
- So '\112' and '\x4A' are both a capitol 'J'. See the ASCII table to confirm.
Character Arithmetic
- Because chars are really small integers, it is possible to do mathematical operations on them. For example:
char letter = 'G', lower, upper; // Presume lower has been given a value somehow letter = letter + 3; // letter has now been changed from 'G' to 'J' if( lower >= 'a' && lower <= 'z' ) // If lower is a lower-case letter upper = lower + ( 'A' - 'a' ) // Convert it to upper-case by adding an offset
Implicit
average = nTotal / 3.0;
average = ( double ) nTotal / nStudents;
For each of the constants in the following table, indicate whether the constant is legal or illegal, what type of constant it is if legal, and why illegal otherwise:
Constant | Legal? |
Explanation |
486 |
Yes |
Decimal int |
98.6 |
Yes |
Double |
98.6f |
Yes |
Float - The f indicates float, as opposed to double. |
02.479 |
No |
No decimal allowed in an octal constant |
0.2479 |
Yes |
Double |
"A" |
Yes |
Character string - Two chars including the null byte. |
'A' |
Yes |
A single char. No null byte. |
'ABC' |
No |
Single quotes are for single chars only |
'\n' |
Yes |
The escape sequence represents a single newline character |
000042 |
Yes |
Octal. 4 * 8 + 2 = 34 in decimal |
0ffH |
No |
No 'f' in octal. ( Resembles an old FORTRAN format. ) |
0xC2F9 |
Yes |
Hexadecimal |
+37.1 |
Yes |
Double |
.0000 |
Yes |
Double |
0xFLU |
Yes |
Hexidecimal. Value = 15, long, unsigned |
0x48.6 |
No |
No decimal point allowed in hexidecimal |
0x486e1 |
Yes |
'e' is a valid hexidecimal digit |
486e1 |
Yes |
Double. 486 * 10^1 = 4860 |
0486e1 |
No |
No 'e' allowed in octal |
0486 |
No |
No '8' allowed in octal either. :-) |
enum SizeType { small, medium, large }; // Declares a new data type, "SizeType" SizeType item; // Declares a variable of type "SizeType" // ( Some code left out here. ) if( num < 25 ) item = small; // Use as an int, using the named values instead of numbers cout << "\nThe item is "; switch( item ) { case small: // Named values are valid integers printf( "tiny\n" ); break;
enum errorType { none = 0, minor1 = 1, minor2, major1 = 100, major2, fatal1 = 1000 };
errorType errorCode = none; sizeType bookSize = large;