programing

크기 뒤에 괄호를 사용해야 하는 이유와 시기

prostudy 2022. 6. 5. 17:47
반응형

크기 뒤에 괄호를 사용해야 하는 이유와 시기

아래는 컴파일 할 수 없습니다.

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

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

근데 만약에 제가 이걸 바꾸면

sizeof(arr);

다 괜찮아, 왜?

6.5.3에 따르면,sizeof다음과 같습니다.

sizeof unary-expression
sizeof ( type-name )

부터arr당신의 암호는type-name괄호로 묶어야 합니다.

언어를 지정하는 방법이며, 유형 이름은 괄호로 묶어야 합니다.

문법은 다음과 같습니다.

sizeof unary-expression sizeof type-name

예를 들어 다음과 같은 표현은 애매합니다.

sizeof int * + 0

둘 중 하나일 수도 있다.sizeof(int *) + 0또는sizeof(int) * +0단항식에는 이 모호성이 발생하지 않습니다.식에는 아스타리스크가 식에 추가되어 있지 않기 때문입니다(다만, 타입명에 따라서는, 1을 추가하는 것도 타입명이 됩니다).

여기에 무언가를 지정해야 하고, 타입명을 괄호로 묶어야 하는 것이 애매함을 해소하는 방법입니다.

내 생각엔 네가...typedef삭제하면 컴파일이 됩니다.

위키피디아에서의 예:

/* the following code fragment illustrates the use of sizeof     
 * with variables and expressions (no parentheses needed),
 * and with type names (parentheses needed)    
 */
char c;
printf("%zu,%zu\n", sizeof c, sizeof (int));

언급URL : https://stackoverflow.com/questions/5894892/why-and-when-do-i-need-to-use-parentheses-after-sizeof

반응형