Phil CK

Sizeof Empty Struct

Last updated on

This was asked of me in an interview and I had no idea what it was.

What is the result of this program

#include <stdio.h>
struct foo {};

int main() {
        printf("sizeof(struct foo) = %d\n", (int)sizeof(struct foo));
        return 0;
}

Well the answer depends on the language.

Compile and Run in C

cc sizeof_test.c && ./a.out
sizeof(struct foo) = 0;

Compile and Run in C++

c++ sizeof_test.cpp && ./a.out
sizeof(struct foo) = 1;

For a variety of reasons the results are not all together unsurprising.