Home > OS >  How to separate format specifiers from characters with printf
How to separate format specifiers from characters with printf

Time:09-05

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

CodePudding user response:

Just write

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

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

CodePudding user response:

try printf("%luus", microseconds); // note exta 'u' here output will be '100us' as you intended.

  • Related