printf("short:%d\n",sizeof(short)); printf("double:%d\n",sizeof(double)); printf("long double:%d\n",sizeof(long double)); printf("long:%d\n",sizeof(long)); printf("int:%d\n",sizeof(int)); printf("float:%d\n",sizeof(float)); printf("char:%d\n",sizeof(char));
As long as it is a pointer, including char *, sizeof is 4.
When sizeof is used to evaluate the array name, it is not regarded as a pointer. sizeof is the actual number of bytes allocated to the array. The char [] type array also follows this rule, the number of array elements can be implicitly specified through the initialization table.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------
The above discussion targets 32-bit CPUs.
The 8-byte 32-bit cpu type is double, long double, and long.
The 64-bit cpu pointer and the type with a long value change to twice the original value, and the others remain unchanged (including double ).
The running result is as follows: (the difference between the two is marked with blue and red respectively)
--------------------------
Machine Platform: X86_64 Processor
Operating System: Red Hat 4.1.2-14
Compilers: gcc version 4.1.2 20070626
Size of char is: 1
Size of unsigned char is: 1
Size of signed char is: 1
Size of int is: 4
Size of short is: 2
Size of long is: 8
Size of long Int Is: 8
Size of signed Int Is: 4
Size of unsigned Int Is: 4
Size of unsigned long Int Is: 8
Size of long Int Is: 8
Size of unsigned long is: 8
Size of float is: 4
Size of double is: 8
Size of Long double is: 16
Size of (void *) is: 8
Size of (char *) is: 8
Size of (int *) is: 8
Size of (long *) is: 8
Size of (long *) is: 8
Size of (float *) is: 8
Size of (double *) is: 8
--------------------------
Machine Platform: x86_32 Processor
Operating System: Windows XP SP2
Compiler: C ++ compiler that comes with vs2005
Size of char is: 1
Size of unsigned char is: 1
Size of signed Char is: 1
Size of int is: 4
Size of short is: 2
Size of long is: 4
Size of long int is: 4
Size of signed int is: 4
Size of unsigned int is: 4
Size of unsigned long int is: 4
Size of long Int Is: 8
Size of unsigned long is: 8
Size of float is: 4
Size of double is: 8
Size of Long double is: 8
Size of (void *) is: 4
Size of (char *) is: 4
Size of (int *) is: 4
Size of (long *) is: 4
Size of (long *) is: 4
Size of (float *) is: 4
Size of (double *) is: 4
---------------------------------------------------------------
(1) Empty struct or class, sizeof size is 1
(2) Static members in c ++ are stored in the global data zone and allocated in the sizeof computing stack. Therefore, they are not included.
(3) the char array is not degraded when it is used as the sizeof parameter, but is degraded to char * when it is used as the strlen parameter *
(4) When an array is used as a function parameter, it degrades to a pointer, so its sizeof returns the pointer length.