Set Decimal Precision
You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point:
Example
float myFloatNum = 3.5;
double myDoubleNum = 19.99;
printf("%f\n", myFloatNum); // Outputs 3.500000
printf("%lf", myDoubleNum); // Outputs 19.990000If you want to remove the extra zeros (set decimal precision), you can use a dot (.) followed by a number that specifies how many digits that should be shown after the decimal point:
Example
float myFloatNum = 3.5;
printf("%f\n", myFloatNum); // Default will show 6 digits after the decimal point
printf("%.1f\n", myFloatNum); // Only show 1 digit
printf("%.2f\n", myFloatNum); // Only show 2 digits
printf("%.4f", myFloatNum); // Only show 4 digits
Get the Memory Size
We introduced in the data types chapter that the memory size of a variable varies depending on the type:
| Data Type | Size |
|---|---|
int | 2 or 4 bytes |
float | 4 bytes |
double | 8 bytes |
char | 1 byte |
The memory size refers to how much space a type occupies in the computer's memory.
To actually get the size (in bytes) of a data type or variable, use the sizeof operator:
Example
int myInt;
float myFloat;
double myDouble;
char myChar;
printf("%zu\n", sizeof(myInt));
printf("%zu\n", sizeof(myFloat));
printf("%zu\n", sizeof(myDouble));
printf("%zu\n", sizeof(myChar));Note that we use the %zu format specifier to print the result, instead of %d. This is because the compiler expects the sizeof operator to return a value of type size_t, which is an unsigned integer type. On some computers it might work with %d, but it is safer and more portable to use %zu, which is specifically designed for printing size_t values.
Why Should I Know the Size of Data Types?
Knowing the size of data types helps you understand how much memory your program uses. This is important when writing larger programs or working with limited memory, because it can affect both performance and efficiency.
For example, the size of a char type is 1 byte. Which means if you have an array of 1000 char values, it will occupy 1000 bytes (1 KB) of memory.
Using the right data type for the right purpose will save memory and improve the performance of your program.
You will learn more about the sizeof operator later in this tutorial, and how to use it in different scenarios.
Use int when
Real-Life Example
Here's a real-life example of using different data types, to calculate and output the total cost of a number of items:
Example
// Create variables of different data types
int items = 50;
float cost_per_item = 9.99;
float total_cost = items * cost_per_item;
char currency = '$';
// Print variables
printf("Number of items: %d\n", items);
printf("Cost per item: %.2f %c\n", cost_per_item, currency);
printf("Total cost = %.2f %c\n", total_cost, currency);you need to store
More Data Types
Besides the basic types (int, float, double, char), C also gives you extended keywords (short, long, unsigned) to control how large the number is, or whether it can be negative:
| Type | Size* | Range (commonly) | Format Specifier |
|---|---|---|---|
short int | 2 bytes | -32,768 to 32,767 | %hd |
unsigned int | 2 or 4 bytes | 0 to 65,535 (2 bytes) 0 to 4,294,967,295 (4 bytes) | %u |
long int | 4 or 8 bytes | -2,147,483,648 to 2,147,483,647 (4 bytes) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (8 bytes) | %ld |
long long int | 8 bytes | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | %lld |
unsigned long int | 4 or 8 bytes | 0 to 4,294,967,295 (4 bytes) 0 to 18,446,744,073,709,551,615 (8 bytes) | %lu |
unsigned long long int | 8 bytes | 0 to 18,446,744,073,709,551,615 | %llu |
long double | 8, 12, or 16 bytes | Implementation-dependent, but more precision than double | %Lf |
unsigned means the type can only store non-negative values (0 and up).
*Note: The size of these types can differ between systems (for example, 2 or 4 bytes, or 4 or 8 bytes), depending on whether the computer is older or newer, 32-bit or 64-bit, and which compiler is used.
Here is an example of how you can create and print these extended types:
Example
int normalInt = 1000; // standard int
double normalDouble = 3.14; // standard double
short int small = -100; // smaller int
unsigned int count = 25; // only positive int
long int big = 1234567890; // larger int
long long int veryBig = 9223372036854775807; // very large int
unsigned long long int huge = 18446744073709551615U; // very large, only positive
long double precise = 3.141592653589793238L; // extended precision
printf("Normal int: %d\n", normalInt);
printf("Normal double: %lf\n", normalDouble);
printf("Small: %hd\n", small);
printf("Count: %u\n", count);
printf("Big: %ld\n", big);
printf("Very Big: %lld\n", veryBig);
printf("Huge: %llu\n", huge);
printf("Precise: %Lf\n", precise);
Note: These extended types are mostly used when you need very specific control over memory usage or number ranges.
Note: For everyday programming, int, float, double, and char are usually enough.
a whole number witho
Check Size
*The exact sizes depend on your system and compiler. Remember that you can check them with the sizeof operator:
Example
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of short int: %zu bytes\n", sizeof(short int));
printf("Size of unsigned int: %zu bytes\n", sizeof(unsigned int));
printf("Size of long int: %zu bytes\n", sizeof(long int));
printf("Size of long long int: %zu bytes\n", sizeof(long long int));
printf("Size of unsigned long long int: %zu bytes\n", sizeof(unsigned long long int));
printf("Size of long double: %zu bytes\n", sizeof(long double));
Tip: When you really need exact control over the size of your numbers, you can use the fixed-width types, which you will learn about later.
Note: This page is just to show you that C has other extended types, but for now we will keep focusing on the basic types (int, float, double, char) in this tutorial.
ut decimals, like 35 or 1000, and float or double when you need a floating point number (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 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 - but note that it takes up twice as much memory as float (8 bytes vs. 4 bytes).
Scientific Numbers
In C, you can write very large or very small floating-point numbers using scientific notation.
This is done using the letter e (or E), which stands for "times 10 to the power of".
For example, 35e3 means 35 × 10³ = 35000.
This is useful for writing numbers in a shorter way. Especially when working with scientific values or large-scale data.
Example
float f1 = 35e3; // 35 * 10^3 = 35000
double d1 = 12E4; // 12 * 10^4 = 120000
printf("%f\n", f1);
printf("%lf", d1);
No comments:
Post a Comment