Why (and when) do I need to use parentheses after sizeof?

blueshift picture blueshift · May 5, 2011 · Viewed 12.4k times · Source

The below fails to compile:

typedef int arr[10];
int main(void) {
    return sizeof arr;
}

sizeof.c:3: error: expected expression before ‘arr’

but if I change it to

sizeof(arr);

everything is fine. Why?

Answer

Ise Wisteria picture Ise Wisteria · May 5, 2011

According to 6.5.3, there are two forms for sizeof as the following:

sizeof unary-expression
sizeof ( type-name )

Since arr in your code is a type-name, it has to be parenthesized.