Skip to content

Instantly share code, notes, and snippets.

@brandon1024
Last active December 25, 2020 17:38
Show Gist options
  • Save brandon1024/a0d1af7d52837556df65f724b26149b5 to your computer and use it in GitHub Desktop.
Save brandon1024/a0d1af7d52837556df65f724b26149b5 to your computer and use it in GitHub Desktop.
Common Format String Directives for Various Data Types in C

Common Format String Directives for Various Data Types in C

I find myself often having trouble picking the right format string specifiers (or directives), so this is a cheat sheet. Maybe this can help others too.

These lists are not exhaustive (and not guarenteed to be platform agnostic), but will be updated as I encounter new types and directives.

Standard Data Types

  • char %c (or %hhi for numerical)
    • signed char
  • unsigned char %c (or %hhu for numerical)
  • short %hi
    • short int
    • signed short
    • signed short int
  • unsigned short %hu
    • unsigned short int
  • int %d (or %i)
    • int
    • signed
    • signed int
  • unsigned %u
    • unsigned int
  • long %li
    • long int
    • signed long
    • signed long int
  • unsigned long %lu
    • unsigned long int
  • long long %lli
    • long long int
    • signed long long
    • signed long long int
  • unsigned long long %llu
    • unsigned long long int
  • float %f
  • double %lf
  • long double %Lf

Pointers

  • void * %p

<sys/types.h>

  • off_t
    • cast to long int and use %li
  • pid_t
    • casst to long int and use %li
  • mode_t
    • print in octal %o
  • size_t %zu
  • ssize_t %zi

Kernel printk

If variable is of Type,         use printk format specifier:
------------------------------------------------------------
        char                    %d or %x
        unsigned char           %u or %x
        short int               %d or %x
        unsigned short int      %u or %x
        int                     %d or %x
        unsigned int            %u or %x
        long                    %ld or %lx
        unsigned long           %lu or %lx
        long long               %lld or %llx
        unsigned long long      %llu or %llx
        size_t                  %zu or %zx
        ssize_t                 %zd or %zx
        s8                      %d or %x
        u8                      %u or %x
        s16                     %d or %x
        u16                     %u or %x
        s32                     %d or %x
        u32                     %u or %x
        s64                     %lld or %llx
        u64                     %llu or %llx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment