Understanding Numeric Data Types in C: Storing Different Kinds of Numbers
Explore the various numeric data types in C, including int for whole numbers and float or double for floating point numbers with decimals. Learn how to choose the appropriate type based on the values you need to store, such as 35, 1000, 9.99, or 3.14515.
C Numeric Data Types
Numeric Types
In C, you can use different numeric types to store various kinds of numbers. Use int when you need to store a whole number without decimals, such as 35 or 1000. Use float or double when you need a floating point number (with decimals), like 9.99 or 3.14515.
Integer Type
Example
int myNum = 1000;
printf("%d", myNum);
Float Type
Example
float myNum = 5.75;
printf("%f", myNum);
Double Type
Example
double myNum = 19.99;
printf("%lf", myNum);
Float vs. Double
The precision of a floating point value indicates how many digits the value can have after the decimal point. The precision of float is six or seven decimal digits, while double variables have a precision of about 15 digits. Therefore, it is often safer to use double for most calculations. However, keep in mind that double takes up 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 indicate the power of 10:
Example
float f1 = 35e3;
double d1 = 12E4;
printf("%f\n", f1);
printf("%lf", d1);
Output Examples
Output
1000
5.750000
19.990000
35000.000000
120000.000000
This demonstrates how numeric data types in C can be used to store and manipulate different types of numbers.