This might be a ver "noob-ish" question, but I've searched through the internet and couldn't find an answer. I know that in C, each type of variable is of a different size:

char   : 1 byte
short  : 2 bytes
int    : 4 bytes
long   : 4 bytes
float  : 4 bytes
double : 8 bytes

But let's say for example, I declare a variable a of tpye float and a variable b also of type float. Now let's say variable a gets assoicated a floating point number such as 130.25 and b gets associated 5.5. Now, taking into account that floats are 4 bytes long, surely a and b are not of the same size once I associate them their respective values. Is that correct? Or are the variables of the same size regardless of their values assoicated to them?

有帮助吗?

解决方案

A variable's size depends on its type, not on its value.

Two float variables will always be the same size, regardless of the value stored in them.

Notice that you can do e.g.:

float a;
float b;

if(sizeof a == sizeof b)
  printf("a and b have the same size\n");

The above is valid even if, as shown, the variables' values are undefined. This is because sizeof can be evaluated at compile-time, and thus not depend on the actual values at all. This is another proof that the value does not affect the size of the variable.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top