+ 2
C program to find size...can anyone tell me how this program works?
3 ответов
+ 6
sizeof returns the memory size of the value you pass to it.
unsigned size1 = sizeof(10.2);
returns 8, because the number is of type double which is of size 8 (bytes).
unsigned size2 = sizeof(010);
returns 4 because 010 is an octal number literal, which translates to a normal int which is of size 4.
Most interesting is line 3:
unsigned size3 = sizeof(ch+d);
ch is a char, which is of size 1, but d is a double.
When you add two numbers (char is represented as a letter, but internally it is a number) of different types, the smaller type is 'upgraded' first, so the result will be of the larger type, in this case double, so 8 bytes.
+ 1
An int variable, for example, is typically allocated 4 bytes when declared. We know this by using the sizeof operator:
Exam:
int x;
Printf ("%d" , sizeof (x));