2017-10-21 2 views
3

x86 및 x86_64 시스템에서 int16_t을 컴파일하는 동안 int16_t complex이 컴파일되지 않는 이유는 short int에 typedef가 있습니까? 다음은 g99 5.4 및 4.9에서 C99 및 C11 표준을 사용하여 테스트 한 샘플 코드입니다. 컴파일러는 선언 지정자에 두 개 이상의 데이터 유형이있는 것에 대해 불만을 토로합니다.왜 'int16_t complex'가 작동하지 않습니까?

코드 :

#include <complex.h> 
#include <stdint.h> 
#include <stdio.h> 

int main() 
{ 
    float complex x = I + I/3 * I/2; 
    short int complex y = I + I/3 * I/2; 
    int16_t complex z = I + I/3 * I/2;  /* Why ? */ 
    printf("x=(%+f,%+f)\n", creal(x), cimag(x)); 
    printf("y=(%+f,%+f)\n", creal(y), cimag(y)); 
    printf("z=(%+f,%+f)\n", creal(z), cimag(z)); /* Why ? */ 
    return 0; 
} 

오류 :

In file included from ./complex.c:1:0: 
./complex.c: In function ‘main’: 
./complex.c:9:13: error: two or more data types in declaration specifiers 
    int16_t complex z = I + I/3 * I/2;  /* Why ? */ 
는 는 컴파일러 명령 줄

:

gcc-5 --std=c99 ./complex.c -o ./complex 
gcc-4.9 --std=c99 ./complex.c -o ./complex 
+0

C99 모드 ('-std = c99')로 실행할 때'short int complex'가 허용됩니까? 경고 수준을 높이는 것을 고려하십시오. – alk

+0

@alk 예. '--pedantic'과'-Wall -Werror'도 있습니다. –

답변

6

우선, 복잡한 정수 유형은 GCC 확장입니다. C 표준 말한다 (C11 6.2.5p11) :

11 There are three complex types, designated as float _Complex , double _Complex , and long double _Complex .43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types.

_Complex 그냥 long 같은 형식 이름의 일부입니다. 당신은이 작업을 수행 할 수 없습니다

typedef double dbl; 
typedef long dbl ldbl; 

즉, 두 번에 대한 형식 정의를 사용하는 경우 long double에 대한 ldbl에 대한 typedef을 정의하려고! 마찬가지로 복합 유형을 정의 할 때 typedef (예 : int16_t)을 사용할 수 없으면 short int _Complex을 대신 사용하십시오.

(물론 이것은 _Complex으로 확장되는 매크로이기 때문에 complex에도 적용됩니다.)

3

있다 간단하기 때문에 이러한 유형은 int16_t complex입니다. C11에서 유효한 복합 유형은 float complex, double complexlong double complex입니다.

complex은 실제 형식 지정자 인 매크로 which expands to _Complex입니다.

+0

그럼 왜''short int complex''와''int complex''가 작동합니까? –

+4

GCC는 [복합 정수 유형] (https://gcc.gnu.org/onlinedocs/gcc/Complex.html)을 확장 기능으로 지원하지만 비표준입니다. –

4

_Complex은 형식 이름 자체의 일부이며 다른 형식과 함께 사용할 수있는 한정자가 아니기 때문입니다. short뿐만 아니라 어떤 종류의 typedef에서도 작동하지 않습니다. 예를 들어, 이렇게하면

typedef float fp32; 
fp32 _Complex x = I + I/3 * I/2; 

같은 컴파일 오류가 표시됩니다.

+0

재미 있습니다. C++/CLI에서 영감을받은이 간격 키워드 개념은 무엇입니까? – IllidanS4

+0

@ IllidanS4는 '서명되지 않은 문자'에 대해 들어 본 적이 있습니다 ... ** ** **입니다. –

+0

@AnttiHaapala 그러나 '서명되지 않은'키워드입니다, 안 그래? 그것은 여러 곳에서 수식어로 명시되어 있습니다. – IllidanS4