Learn How to Program in Cby
|
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:
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:
Double and Long Double Floating PointsLike integers, floating point variables have three sizes:
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 NumbersIf 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:
Example ProgramHere is an example that uses both a regular integer and a double floating point number:
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. |
||||||||||||||||||||
|
Copyright © 2001 Kristin Switala |
|||||||||||||||||||||