How to printf "unsigned long" in C? – C

Photo of author
Written By M Ibrahim
abstract-class format-specifiers long-integer printf unsigned

Quick Fix: %lu is the format specifier for unsigned long in C’s printf. Make sure to include the correct header <inttypes.h> for this to work.

The Solutions:

Solution 1: Use the correct format specifier

The correct format specifier for printing an unsigned long is `%lu`.

Solution 2: Use the correct format specifier for unsigned long

The correct format specifier for printing an unsigned long is %lu. The other format specifiers you tried are for different data types:

  • %d is for signed integers
  • %ld is for signed long integers
  • %ll is for signed long long integers
  • %dl is not a valid format specifier

Here’s an example of how to print an unsigned long:

unsigned long unsigned_foo = 1234567890;
printf("%lu\n", unsigned_foo); // prints 1234567890

Solution 3:

To print an unsigned long in C, use the format specifier `%lu`. For example:

#include <stdio.h>

int main() {
  unsigned long unsigned_foo = 1234567890;
  printf("%lu\n", unsigned_foo);  // prints 1234567890
}

Solution 4: Remove %d family format specifiers

Out of all the combinations you tried, %ld and %lu are the only ones which are valid printf format specifiers at all. %lu (long unsigned decimal), %lx or %lX (long hex with lowercase or uppercase letters), and %lo (long octal) are the only valid format specifiers for a variable of type unsigned long (of course you can add field width, precision, etc modifiers between the % and the l).