The sizeof operator in C language gets the number of bytes of a data type or variable. It can act on data types, variable names, array names, structure or union types. The returned value is the number of bytes occupied by the data type or variable, in bytes. It is used to determine memory allocations, calculate array or structure sizes, verify data type compatibility, and implement portable code.
Usage of sizeof
in C language
sizeof
is An operator in C language used to obtain the number of bytes occupied by a data type or variable in memory. It returns an integer in bytes.
Usage
sizeof
is followed by a parentheses, which can be:
sizeof(int)
)sizeof(myVariable)
)sizeof(myArray)
) sizeof(myStructure)
) Return value
sizeof
The returned value is the number of bytes occupied by the data type or variable in memory. For example, on 32-bit systems, sizeof(int)
usually returns 4 because the int
type occupies 4 bytes.
Uses
sizeof
has many uses, including:
Example
<code class="c">#include <stdio.h> int main() { printf("int size: %ld\n", sizeof(int)); printf("float size: %ld\n", sizeof(float)); printf("double size: %ld\n", sizeof(double)); printf("char size: %ld\n", sizeof(char)); int myVariable = 123; printf("myVariable size: %ld\n", sizeof(myVariable)); return 0; }</code>
Output:
<code>int size: 4 float size: 4 double size: 8 char size: 1 myVariable size: 4</code>
The above is the detailed content of How to calculate sizeof in c language. For more information, please follow other related articles on the PHP Chinese website!