¿Cómo formatear un long long int sin firmar usando 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;
}
Salida:
My number is 8 bytes wide and its value is 285212672l. A normal number is 0.
Asumo que este resultado inesperado es de imprimir el unsigned long long int
. ¿Cómo haces printf()
un unsigned long long int
?
11 answers
Use el modificador ll (el-el) long-long con la conversión u (sin signo). (Funciona en Windows, GNU).
printf("%llu", 285212672);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-25 22:13:14
Puede intentar usar los inttypes.h biblioteca que le da tipos como
int32_t
, int64_t
, uint64_t
etc.
A continuación, puede utilizar sus macros como:
uint64_t x;
uint32_t y;
printf("x: %"PRId64", y: %"PRId32"\n", x, y);
Esto está "garantizado" para no darle el mismo problema que long
, unsigned long long
etc, ya que no tiene que adivinar cuántos bits hay en cada tipo de datos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-09-04 19:58:07
%d
--> para int
%u
--> para unsigned int
%ld
--> para long int
%lu
--> para unsigned long int
%lld
--> para long long int
%llu
--> para unsigned long long int
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-06 05:21:09
Para long long (o __int64) usando MSVS, debe usar %I64d:
__int64 a;
time_t b;
...
fprintf(outFile,"%I64d,%I64d\n",a,b); //I is capital i
Esto se debe a que %llu no funciona correctamente bajo Windows y %d no puede manejar enteros de 64 bits. Sugiero usar PRIu64 en su lugar y encontrarás que también es portable a Linux.
Prueba esto en su lugar:
#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;
}
Salida
My number is 8 bytes wide and its value is 285212672. A normal number is 5.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-27 19:08:36
En Linux es %llu
y en Windows es %I64u
Aunque he encontrado que no funciona en Windows 2000, parece que hay un error allí!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-13 09:42:28
Compilar como x64 con VS2005:
%llu funciona bien.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-11 06:23:13
Las cosas no estándar son siempre extrañas:)
Para la porción larga larga
bajo GNU es L
, ll
o q
Y bajo Windows creo que es ll
solo
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-05 21:03:07
Hex:
printf("64bit: %llp", 0xffffffffffffffff);
Salida:
64bit: FFFFFFFFFFFFFFFF
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-26 09:57:06
Además de lo que la gente escribió hace años:
- podría obtener este error en gcc / mingw:
main.c:30:3: warning: unknown conversion type character 'l' in format [-Wformat=]
printf("%llu\n", k);
Entonces su versión de mingw no tiene por defecto c99. Agregue esta bandera de compilador: -std=c99
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-17 12:27:29
Bueno, una forma es compilarlo como x64 con VS2008
Esto se ejecuta como es de esperar:
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);
Para código de 32 bits, necesitamos usar el especificador de formato __int64 %I64u correcto.
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);
Este código funciona tanto para compilador VS de 32 como de 64 bits.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-06-25 16:23:00