In the data types chapter, we discussed how the memory size of a variable fluctuates based on its type.
Data Type |
Size |
|
2 or 4 bytes |
|
4 bytes |
|
8 bytes |
|
1 byte |
Memory size denotes the space occupied by a data type in the computer’s memory.
To obtain the size, measured in bytes, of a data type or variable, utilize the sizeof operator.
int myInt; float myFloat; double myDouble; char myChar; printf(“%lu\n”, sizeof(myInt)); |
It’s important to note that we employ the %lu format specifier to print the result of the sizeof operator instead of %d. This is because the compiler anticipates the sizeof operator to return a long unsigned int (%lu) rather than an int (%d). While %d might function on certain computers, it’s more reliable to use %lu. Why Should I Know the Size of Data Types?Utilizing the appropriate data type for the intended purpose conserves memory and enhances program performance. Later in this tutorial, you’ll delve deeper into the sizeof operator and its various applications. |