C에서 int64_t 타입을 인쇄하는 방법
C99 표준에는 int64_t와 같은 바이트 크기의 정수형이 있습니다.Windows 를 사용하고 있습니다.%I64d
현재(또는 서명되지 않음) 포맷%I64u
)는 다음과 같습니다.
#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);
다음 컴파일러 경고가 표시됩니다.
warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’
시도 대상:
printf("This is my_int: %lld\n", my_int); // long long decimal
하지만 같은 경고를 받았어요.이 컴파일러를 사용하고 있습니다.
~/dev/c$ cc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
경고 없이 my_int 변수를 인쇄하려면 어떤 형식을 사용해야 합니까?
위해서int64_t
입력:
#include <inttypes.h>
int64_t t;
printf("%" PRId64 "\n", t);
위해서uint64_t
입력:
#include <inttypes.h>
uint64_t t;
printf("%" PRIu64 "\n", t);
를 사용할 수도 있습니다.PRIx64
16진수로 인쇄합니다.
cppreference.com에는 다음과 같은 모든 유형의 사용 가능한 매크로 목록이 있습니다.intptr_t
(PRIxPTR
) scanf에는 다음과 같은 매크로가 별도로 있습니다.SCNd64
.
PRIu16의 일반적인 정의는 다음과 같습니다."hu"
따라서 컴파일 시에 암묵적인 스트링과 스트링의 결합이 이루어집니다.
코드를 완전히 이식하려면PRId32
인쇄 등int32_t
,그리고."%d"
또는 이와 유사한 인쇄int
.
C99 방법은
#include <inttypes.h>
int64_t my_int = 999999999999999999;
printf("%" PRId64 "\n", my_int);
아니면 캐스팅을 할 수도 있어!
printf("%ld", (long)my_int);
printf("%lld", (long long)my_int); /* C89 didn't define `long long` */
printf("%f", (double)my_int);
C89 실장(특히 Visual Studio)이 어려운 경우 오픈 소스를 사용할 수 있습니다.<inttypes.h>
(그리고<stdint.h>
): http://code.google.com/p/msinttypes/
uclibc조차 항상 사용할 수 없는 임베디드 세계에서 온 것으로, 다음과 같은 코드입니다.
uint64_t myval = 0xdeadfacedeadbeef; printf("%llx", myval);
인쇄가 안 되거나 전혀 작동하지 않습니다.항상 작은 도우미를 사용하고 있기 때문에 uint64_t 16진수를 적절히 덤프할 수 있습니다.
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
char* ullx(uint64_t val)
{
static char buf[34] = { [0 ... 33] = 0 };
char* out = &buf[33];
uint64_t hval = val;
unsigned int hbase = 16;
do {
*out = "0123456789abcdef"[hval % hbase];
--out;
hval /= hbase;
} while(hval);
*out-- = 'x', *out = '0';
return out;
}
Windows 환경에서 사용
%I64d
Linux의 경우, 사용
%lld
C99의 경우%j
길이 수식자는 printf 함수 패밀리와 함께 사용하여 유형의 값을 인쇄할 수도 있습니다.int64_t
그리고.uint64_t
:
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
int64_t a = 1LL << 63;
uint64_t b = 1ULL << 63;
printf("a=%jd (0x%jx)\n", a, a);
printf("b=%ju (0x%jx)\n", b, b);
return 0;
}
이 코드의 컴파일gcc -Wall -pedantic -std=c99
는 경고를 생성하지 않으며 프로그램은 예상 출력을 출력합니다.
a=-9223372036854775808 (0x8000000000000000)
b=9223372036854775808 (0x8000000000000000)
이것은 에 의거한 것이다.printf(3)
(man 페이지에는 특별히 다음과 같이 되어 있습니다)j
에의 변환을 나타내기 위해서 사용됩니다.intmax_t
또는uintmax_t
내 stdint.h에서는 둘 다int64_t
그리고.intmax_t
typedef'd는 정확히 같은 방법으로, 그리고 마찬가지로uint64_t
). 다른 시스템에 완벽하게 휴대할 수 있는지 잘 모르겠습니다.
//VC6.0(386 이상)
__int64 my_qw_var = 0x1234567890abcdef;
__int32 v_dw_h;
__int32 v_dw_l;
__asm
{
mov eax,[dword ptr my_qw_var + 4] //dwh
mov [dword ptr v_dw_h],eax
mov eax,[dword ptr my_qw_var] //dwl
mov [dword ptr v_dw_l],eax
}
//Oops 0.8 format
printf("val = 0x%0.8x%0.8x\n", (__int32)v_dw_h, (__int32)v_dw_l);
안부 전해요.
언급URL : https://stackoverflow.com/questions/9225567/how-to-print-a-int64-t-type-in-c
'programing' 카테고리의 다른 글
mariadb가 WSREP를 시작할 수 없음: std:: bad_alloc (0) | 2022.10.22 |
---|---|
루트를 바꾸면 노래가 안 나와요.다이내믹 오디오 만드는 법 (0) | 2022.10.22 |
Eclipse에서 maven 저장소를 업데이트하려면 어떻게 해야 합니까? (0) | 2022.10.22 |
vuejs 2에서 구성 요소의 내부 html을 변수로 가져옵니다. (0) | 2022.10.22 |
find(), findOrFail(), first(), firstOrFail(), get(), list() 및 toArray()의 차이점은 무엇입니까? (0) | 2022.10.22 |