sizeof operators and size_t types
The role of *sizeof
sizeof is one of the operators of C, which is used to get the amount of memory space that the operand is allocated in byte units.
The operands here can be variables or data types such as int,float. So it's possible to get a range of the basic types defined by the local C library.
The use of *sizeof
1. For general variables, form 2 kinds: sizeof A or sizeof (a);
2. For data types, you must use parenthesized methods, such as sizeof (int).
Description of *size_t
SIZE_T is defined in the standard C library and should be unsigned int, which is long unsigned int in a 64-bit system.
sizeof returns must be an unsigned shape, in standard C the return value type is defined as size_t by a typedef.
If you output the size_t type with printf, the format character%zd is defined in C99, and you can try%u or%lu if the compiler does not support it.
* Comparison with Strlen
sizeof, gets the number of bytes of memory space occupied by the operand, and returns the type size_t;
Strlen, gets the number of bytes actually used by the character array, does not contain the end of the array ' and ', the return type size_t;
As shown:
#include <stdio.h>
#include <string.h>
int main (void)
{
int a = 1;
Char b[4] = "AAA";
printf ("%zd%zd%lu\n", sizeof a,sizeof (int), sizeof a);
printf ("%zd%zd\n", sizeof B,strlen (b));
return 0;
}
Results:
4 4 4
4 3