Example:
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
int main(int argc,char **argv)? //main program
{
//Test the number of bytes occupied by built-in types in C language.
Printf(" char size of char is: %d\n ",sizeof(char));); //Number of bytes of character data,
Printf(" short of short: %d\n ",sizeof(short));); ? //Number of bytes of short shaping data
Printf ("the size of int is: %d\n", sizeof(int));); //shape data
Printf ("the size of short int is: %d\n", sizeof(short int));); //short shaping
Printf(" long int's size is: %d\n ",sizeof(long int));); //Long integer data
Printf ("the size of float is: %d\n", size of (float)); ? //floating point data
Printf ("size of double: %d\n", sizeof(double));); //Double precision data
//Measure the number of bytes occupied by character and string arrays.
char a[] ={'a ',' b ',' c ',' d ',' e ' };
Printf (the size of "a [] is: %d\n", sizeof(a));); //Number of bytes occupied by output array a[]
char b[]= { " abcde " }; ? //Initialize string b
Printf (size of "b [] is: %d\n", sizeof(b));); //array b[]
char c[][3]={{'a ',' b ',' c'},{'d ',' e ',' f'},{'g ',' h ',' i'},{'j ',' k ',' l ' } }? //Initialize a two-dimensional character array
Printf (size of "c [] [] is: %d\n", sizeof(c));); ? //Number of bytes occupied by two-dimensional array C
Printf ("the size of c [0] is: %d\n", sizeof (c [0])); //The number of bytes occupied by a line (such as line 0) in a two-dimensional array.
Printf ("the size of c [0] [0] is: %d\n", sizeof (c [0] [0])); ? //An element of a line, such as the 0 th element of line 0.
//A measure of the number of bytes occupied by the pointer.
char * p = 0;
printf("size of *p is: %d\n ",sizeof(p)); //character pointer
System ("suspended");
Returns 0;
}
Extended data
Use sizeof () to calculate the array length.
Using sizeof on expr does not calculate the value of expr. Especially in sizeof *p, the pointer p can be an invalid address because there is no need to dereference p.
The result of sizeof operation on an expression of type char or a value of type char is 1. The Sizeof operation on a reference type will return the amount of memory space required to store the object of the reference type.
Sizeof operation on pointer will return the memory size needed to store pointer. Note that if you want to get the size of the object pointed by the pointer, you must dereference the pointer. Sizeof operation on an array is equivalent to multiplying the result of sizeof operation on its elements by the number of array elements.
Because sizeof returns the storage length of the whole array in memory, the number of array elements can be obtained by using the result of sizeof array as the first result of its element type: int SZ = sizeof (ia)/sizeof (* ia);
Usually an 8-bit block is regarded as a byte, and 32 bits or 4 bytes are regarded as a "word". Generally, short is half a machine word length, and int is a machine word length.