Cómo imprimir f "unsigned long" en C?


Nunca puedo entender cómo imprimir unsigned long tipo de datos en C.

Supongamos que unsigned_foo es un unsigned long, entonces intento:

  • printf("%lu\n", unsigned_foo)
  • printf("%du\n", unsigned_foo)
  • printf("%ud\n", unsigned_foo)
  • printf("%ll\n", unsigned_foo)
  • printf("%ld\n", unsigned_foo)
  • printf("%dl\n", unsigned_foo)

Y todos ellos de impresión algún tipo de -123123123 número en lugar de unsigned long que tengo.

Author: Tardis, 2010-07-09

7 answers

%lu es el formato correcto para unsigned long. Parece que hay otros problemas en juego aquí, como la corrupción de la memoria o una variable no inicializada. Tal vez nos muestran una imagen más grande?

 409
Author: Thanatos,
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-07-09 04:50:19

De todas las combinaciones que probó, %ld y %lu son las únicas que son especificadores de formato printf válidos. %lu (decimal largo sin signo), %lx o %lX (hexadecimal largo con letras minúsculas o mayúsculas), y %lo (octal largo) son los únicos especificadores de formato válidos para una variable de tipo largo sin signo (por supuesto, puede agregar modificadores de ancho de campo, precisión, etc. entre % y l).

 20
Author: R..,
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-07-09 06:00:14
  • %lu for unsigned long
  • %llu for unsigned long long
 17
Author: NealCaffery,
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
2014-07-12 15:01:16

For int %d

For long int %ld

For long long int %lld

For unsigned long long int %llu

 13
Author: Linkon,
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
2017-04-07 10:31:44
int main()
{
    unsigned long long d;
    scanf("%llu",&d);
    printf("%llu",d);
    getch();
}

Esto será útil . . .

 8
Author: Sanjith Bravo Dastan,
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
2013-10-28 03:25:57

El formato es %lu.

Por favor, compruebe los otros tipos de datos y su uso en printf aquí

 8
Author: Praveen S,
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-08-02 17:37:15

El especificador correcto para unsigned long es %lu.

Si no obtiene el valor exacto que espera, puede haber algunos problemas en su código.

Por favor, copie su código aquí. Entonces tal vez alguien pueda decirte mejor cuál es el problema.

 8
Author: Kumar Alok,
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-04-09 18:33:51