cprintfformat-specifiersconversion-specifierlength-modifiers

How to separate format specifiers from characters with printf


Another way to phrase this question might be "How to end format specifiers in printf"

If I want to print microseconds in this format 100us using the following code...

long microseconds = 100L;
printf("%lus", microseconds);

only prints 100s because the u is combined with the %l and it interprets the format specifier as an unsigned long instead of a long


Solution

  • Just write

    long microseconds = 100L;
    printf("%ldus", microseconds);
    

    Pay attention to that you may not use a length modifier without a conversion specifier.