Numeric Types
Employ the int data type for storing whole numbers without decimals, such as 35 or 1000, and use float or double for floating-point numbers with decimals, like 9.99 or 3.14515.
int
int myNum = 1000; printf(“%d”, myNum); |
float
float myNum = 5.75; printf(“%f”, myNum); |
double
double myNum = 19.99; printf(“%lf”, myNum); |
float vs. double
The precision of a floating-point value determines the number of digits it can have after the decimal point. Float variables typically have a precision of six or seven decimal digits, while double variables offer a precision of around 15 digits. Consequently, it’s generally advisable to use double for most calculations due to its higher precision. However, it’s important to note that double requires twice as much memory as float (8 bytes vs. 4 bytes).
|
Scientific Numbers
A floating-point number can also be represented in scientific notation using an “e” to denote the power of 10.
Example
float f1 = 35e3; double d1 = 12E4;
printf(“%f\n”, f1); printf(“%lf”, d1); |