programing

stdint.h와 inttypes.h의 차이점

copysource 2022. 12. 10. 14:25
반응형

stdint.h와 inttypes.h의 차이점

stdint.h와 inttypes.h의 차이점은 무엇입니까?

둘 다 사용되지 않으면 uint64_t는 인식되지 않지만 둘 다 정의된 유형입니다.

stdint.h

지정된 폭의 정수 타입의 C99(즉, 최소 요건)를 사용하는 경우는, 이 파일을 포함한 것이 「최소 요건」입니다.int32_t,uint16_t이 파일을 포함하면 이러한 유형의 정의를 얻을있으므로 변수 및 함수 선언에서 이러한 유형을 사용하고 이러한 데이터 유형을 사용하여 작업을 수행할 수 있습니다.

inttypes.h

이 파일을 포함하면 stdint.h가 제공하는 모든 것을 얻을있지만(inttypes.h에는 stdint.h가 포함되기 때문에), 및 (및)를 실행하기 위한 퍼실리티도 얻을 수 있습니다.fprintf,fscanf, 등)를 휴대할 수 있습니다.예를 들어 다음과 같이 됩니다.PRIu64매크로를 사용하여printf a uint64_t다음과 같습니다.

#include <stdio.h>
#include <inttypes.h>
int main (int argc, char *argv[]) {

    // Only requires stdint.h to compile:
    uint64_t myvar = UINT64_C(0) - UINT64_C(1);

    // Requires inttypes.h to compile:
    printf("myvar=%" PRIu64 "\n", myvar);  
}

사용하고 싶은 이유 중 하나printf예를 들어, inttypes.h는 다음과 같습니다.uint64_tlong unsignedLinux 에서는,long long unsignedWindows 의 경우는, 을 참조해 주세요.따라서 stdint.h(intypes.h가 아님)만 포함할 경우 위의 코드를 작성하여 Linux와 Windows 간에 상호 호환성을 유지하려면 다음 작업을 수행해야 합니다(추악한 #ifdef에 주의).

#include <stdio.h>
#include <stdint.h>
int main (int argc, char *argv[]) {

    // Only requires stdint.h to compile:
    uint64_t myvar = UINT64_C(0) - UINT64_C(1);

    // Not recommended.
    // Requires different cases for different operating systems,
    //  because 'PRIu64' macro is unavailable (only available 
    //  if inttypes.h is #include:d).
    #ifdef __linux__
        printf("myvar=%lu\n", myvar);
    #elif _WIN32
        printf("myvar=%llu\n", myvar);
    #endif
}

inttypes.h에 대한 위키피디아 문서를 참조하십시오.

최소한의 정의 세트에는 stdint.h를 사용합니다.프린트프, 스캔프 등에서 이러한 정의를 휴대용으로 지원하려면 inttypes.h를 사용합니다.

언급URL : https://stackoverflow.com/questions/7597025/difference-between-stdint-h-and-inttypes-h

반응형