Short and Long Integers, Double and Long Double Floating Points
C provides special integer and floating point variables to handle short and long numbers. How you use these depends on how your computer’s memory stores information.
Short and Long Integers
When declaring an integer, you can use one of three variable types:
Value (range of numbers it can hold)
-32,768 to +32,767 or -2,147,438,648 to +2,147,438,647
-32,768 to +32,767
-2,147,438,648 to +2,147,438,647
The int value is strange, because it depends on your computer. If your system holds integers in 2 bytes of memory, then your range is -32,768 to +32,767. If your system holds integers in 4 bytes of memory, then your range is -2,147,438,648 to +2,147,438,648. To be safe, use the types short and long if you know how small or big your number will be.
Here is an example that uses both long and short integers. Notice that the format specifier for a long integer is %ld, whereas we use a regular %d for a short integer:
void main( )
printf(“\nType a number between 300000 and 600000: “);
printf(“\nNow type a number between 1 and 9: “);
total = num / digit;
printf(“\n%ld divided by %d is %ld.\n”, num, digit, total);
Save this source code as eighth.c then compile, link, and run it. If you answered the questions with a long number of 45678 and a short digit of 7, then your output will be:
|45678 divided by 7 is 6525.|
Double and Long Double Floating Points
Like integers, floating point variables have three sizes:
Written in C
|float up to 6 digits
|double up to 15 digits
|long double up to 19 digits
In C we use the E to mean “exponent”. We use the floating point format specifier %f with each of these floating point variables.
Using %.f with Very Large and Very Small Numbers
If you are working with very large or small numbers, you must specify how many decimal and integer places you want to print on the screen. For example:
|Large Number 0.2E7
7 integer places
|Small Number 0.36E-4
4 decimal places
Here is an example that uses both a regular integer and a double floating point number:
void main( )
double acid = 1.3E-9;
int water = 5;
printf(“\nWhat do you get when you mix:”);
printf(“\n%.10f liters of sulfuric acid into %d liters of water?”, acid, water);
printf(“\nA terrible smell!\n”);
In this example, we use %.10f because 9 decimal places plus 1 (for the .3) equals 10. Save your code as acid.c then compile, link, and execute it.
You only have to worry about special integers and floating point numbers if you are working with extremely large or small numbers, for example, in scientific research. For everyday use,int and float work perfectly well.
Continue on to Variables & Strings.