Jak sformatować unsigned long long int używając printf?

#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;
}

Wyjście:

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

Zakładam, że ten nieoczekiwany wynik wynika z wydruku unsigned long long int. Jak ty printf() an unsigned long long int?

Author: trentcl, 2008-08-06

11 answers

Użyj modyfikatora ll (el-el) long-long z konwersją u (unsigned). (Działa w windows, GNU).

printf("%llu", 285212672);
 397
Author: John Downey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-04-25 22:13:14

Możesz spróbować użyć inttypów.biblioteka h, która daje takie typy jak int32_t, int64_t, uint64_t itd. Następnie możesz użyć jego makr, takich jak:

uint64_t x;
uint32_t y;

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

To jest "gwarantowane", aby nie sprawiać ci tych samych problemów, co long, unsigned long long itd, ponieważ nie musisz zgadywać ile bitów jest w każdym typie danych.

 74
Author: Nathan Fellman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-04 19:58:07

%d--> dla int

%u--> dla unsigned int

%ld--> dla long int

%lu--> dla unsigned long int

%lld--> dla long long int

%llu--> dla unsigned long long int

 38
Author: Shivam Chauhan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-06 05:21:09

Dla long long (lub _ _ int64) używając MSVS, powinieneś użyć %i64d:

__int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d\n",a,b);    //I is capital i
 35
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-06 14:36:41

Dzieje się tak dlatego, że %llu nie działa poprawnie pod Windows i %d nie obsługuje 64-bitowych liczb całkowitych. Proponuję zamiast tego użyć PRIu64 i przekonasz się, że jest też Przenośny na Linuksa.

Spróbuj zamiast tego:

#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;
}

Wyjście

My number is 8 bytes wide and its value is 285212672. A normal number is 5.
 30
Author: Paul Hargreaves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-27 19:08:36

W Linuksie jest to %llu, a w Windows jest to %I64u

Chociaż odkryłem, że nie działa w Windows 2000, wydaje się, że jest tam błąd!

 10
Author: Adam Pierce,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-13 09:42:28

Skompiluj go jako x64 z VS2005:

% llu działa dobrze.

 8
Author: Sandy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-11 06:23:13

Niestandardowe rzeczy są zawsze dziwne:)

Dla długiej długiej porcji pod GNU jest L, ll lub q

A pod windows To chyba tylko ll

 4
Author: sparkes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-05 21:03:07

Hex:

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

Wyjście:

64bit: FFFFFFFFFFFFFFFF
 4
Author: lama12345,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-26 09:57:06

Oprócz tego co ludzie napisali przed laty:

  • możesz dostać ten błąd na gcc/mingw:

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

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

Wtedy Twoja wersja mingw nie domyślnie C99. Dodaj tę flagę kompilatora: -std=c99.

 2
Author: Bernd Elkemann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-17 12:27:29

Cóż, jednym ze sposobów jest skompilowanie go jako x64 z VS2008

To działa tak, jak można się spodziewać:

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);

Dla 32-bitowego kodu, musimy użyć właściwego _ _ int64 format specifier %i64u. tak się stanie.

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);

Ten kod działa zarówno dla 32 jak i 64 bitów VS kompilatora.

 1
Author: vzczc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-25 16:23:00