Skip to main content
sizeof() with a VLA is evaluated
Source Link
codaddict
  • 456.9k
  • 83
  • 501
  • 537

sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters.

short func(short x) { // this function never gets called !! printf("%d", x); // this print never happens return x; } int main() { printf("%d", sizeof(func(3))); // all that matters to sizeof is the // return type of the function. return 0; } 

Output:

2 

as short occupies 2 bytes on my machine.

Changing the return type of the function to double:

double func(short x) { // rest all same 

will give 8 as output.

sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated at all; only the type of the result matters.

short func(short x) { // this function never gets called !! printf("%d", x); // this print never happens return x; } int main() { printf("%d", sizeof(func(3))); // all that matters to sizeof is the // return type of the function. return 0; } 

Output:

2 

as short occupies 2 bytes on my machine.

Changing the return type of the function to double:

double func(short x) { // rest all same 

will give 8 as output.

sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated (except when it is a variable length array) at all; only the type of the result matters.

short func(short x) { // this function never gets called !! printf("%d", x); // this print never happens return x; } int main() { printf("%d", sizeof(func(3))); // all that matters to sizeof is the // return type of the function. return 0; } 

Output:

2 

as short occupies 2 bytes on my machine.

Changing the return type of the function to double:

double func(short x) { // rest all same 

will give 8 as output.

added 633 characters in body
Source Link
codaddict
  • 456.9k
  • 83
  • 501
  • 537

sizeof is a compile-time operator, so at the time of compilation, sizeof and its operand get replaced by the result value. The operand is not evaluated at all; only the type of the result matters.

short func(short x) { // this function never gets called !! printf("%d", x); // this print never happens return x; } int main() { printf("%d", sizeof(func(3))); // all that matters to sizeof is the // return type of the function. return 0; } 

Output:

2 

as short occupies 2 bytes on my machine.

Changing the return type of the function to double:

double func(short x) { // rest all same 

will give 8 as output.

sizeof is a compile-time operator so at the time of compilation, sizeof and its operand get replaced by the result value.

sizeof is a compile-time operator, so at the time of compilation sizeof and its operand get replaced by the result value. The operand is not evaluated at all; only the type of the result matters.

short func(short x) { // this function never gets called !! printf("%d", x); // this print never happens return x; } int main() { printf("%d", sizeof(func(3))); // all that matters to sizeof is the // return type of the function. return 0; } 

Output:

2 

as short occupies 2 bytes on my machine.

Changing the return type of the function to double:

double func(short x) { // rest all same 

will give 8 as output.

Source Link
codaddict
  • 456.9k
  • 83
  • 501
  • 537

sizeof is a compile-time operator so at the time of compilation, sizeof and its operand get replaced by the result value.