문제

#include <stdio.h>
int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
    return 0;
}

산출:

My number is 8 bytes wide and its value is 285212672l. A normal number is 0.

나는 이 예상치 못한 결과가 unsigned long long int.어떻게 지내세요? printf()unsigned long long int?

도움이 되었습니까?

해결책

u(부호 없음) 변환과 함께 ll(el-el) long-long 수정자를 사용합니다.(Windows, GNU에서 작동)

printf("%llu", 285212672);

다른 팁

다음과 같은 유형을 제공하는 inttypes.h 라이브러리를 사용해 볼 수도 있습니다.int32_t, int64_t, uint64_t 등.그런 다음 다음과 같은 매크로를 사용할 수 있습니다.

uint64_t x;
uint32_t y;

printf("x: %"PRId64", y: %"PRId32"\n", x, y);

이것은 당신에게 같은 문제를 일으키지 않도록 "보장"됩니다. long, unsigned long long 등, 각 데이터 유형에 몇 비트가 있는지 추측할 필요가 없기 때문입니다.

%d--> int

%u--> unsigned int

%ld--> long int

%lu--> unsigned long int

%lld--> long long int

%llu--> unsigned long long int

MSVS를 사용하는 long long(또는 __int64)의 경우 %I64d를 사용해야 합니다.

__int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d\n",a,b);    //I is capital i

이는 %llu가 Windows에서 제대로 작동하지 않고 %d가 64비트 정수를 처리할 수 없기 때문입니다.대신 PRIu64를 사용하는 것이 좋으며 Linux에도 이식 가능하다는 것을 알게 될 것입니다.

대신 이것을 시도해 보세요:

#include <stdio.h>
#include <inttypes.h>

int main() {
    unsigned long long int num = 285212672; //FYI: fits in 29 bits
    int normalInt = 5;
    /* NOTE: PRIu64 is a preprocessor macro and thus should go outside the quoted string. */
    printf("My number is %d bytes wide and its value is %" PRIu64 ". A normal number is %d.\n", sizeof(num), num, normalInt);
    return 0;
}

산출

My number is 8 bytes wide and its value is 285212672. A normal number is 5.

리눅스에서는 %llu 그리고 Windows에서는 %I64u

Windows 2000에서는 작동하지 않지만 거기에는 버그가 있는 것 같습니다!

VS2005를 사용하여 x64로 컴파일합니다.

%llu는 잘 작동합니다.

비표준적인 것들은 항상 이상합니다 :)

GNU 아래 긴 부분의 경우 L, ll 또는 q

그리고 창문 아래에는 그럴 거라고 믿어요 ll 오직

마녀:

printf("64bit: %llp", 0xffffffffffffffff);

산출:

64bit: FFFFFFFFFFFFFFFF

몇 년 전에 사람들이 쓴 것 외에도 다음과 같은 내용이 있습니다.

  • gcc/mingw에서 다음 오류가 발생할 수 있습니다.

main.c:30:3: warning: unknown conversion type character 'l' in format [-Wformat=]

printf("%llu\n", k);

그러면 mingw 버전이 기본적으로 c99로 설정되지 않습니다.다음 컴파일러 플래그를 추가합니다. -std=c99.

음, 한 가지 방법은 VS2008을 사용하여 x64로 컴파일하는 것입니다.

이는 예상대로 실행됩니다.

int normalInt = 5; 
unsigned long long int num=285212672;
printf(
    "My number is %d bytes wide and its value is %ul. 
    A normal number is %d \n", 
    sizeof(num), 
    num, 
    normalInt);

32비트 코드의 경우 올바른 __int64 형식 지정자 %I64u를 사용해야 합니다.그래서 그것은됩니다.

int normalInt = 5; 
unsigned __int64 num=285212672;
printf(
    "My number is %d bytes wide and its value is %I64u. 
    A normal number is %d", 
    sizeof(num),
    num, normalInt);

이 코드는 32비트 및 64비트 VS 컴파일러 모두에서 작동합니다.

분명히 아무도 멀티 플랫폼* 솔루션을 생각해내지 못했습니다. 10년 넘게 2008년 이후이므로 내 내용을 추가하겠습니다 😛.찬성해주세요.(농담.난 상관없어.)

해결책: lltoa()

사용하는 방법:

#include <stdlib.h> /* lltoa() */
// ...
char dummy[255];
printf("Over 4 bytes: %s\n", lltoa(5555555555, dummy, 10));
printf("Another one: %s\n", lltoa(15555555555, dummy, 10));

OP의 예:

#include <stdio.h>
#include <stdlib.h> /* lltoa() */

int main() {
    unsigned long long int num = 285212672; // fits in 29 bits
    char dummy[255];
    int normalInt = 5;
    printf("My number is %d bytes wide and its value is %s. "
        "A normal number is %d.\n", 
        sizeof(num), lltoa(num, dummy, 10), normalInt);
    return 0;
}

와 달리 %lld 인쇄 형식 문자열, 이것은 나에게 효과적입니다 Windows의 32비트 GCC에서.

*) 잘, 거의 다중 플랫폼.MSVC에서는 분명히 필요합니다. _ui64toa() 대신에 lltoa().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top