C – When to Use Parentheses with sizeof

c++sizeof

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?

Best Answer

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.

Related Question