23

In an example of "C Primer Plus", the author has used %ul format specifier (in both scanf and printf) for unsigned long. When I try to generalize the problem, it seems that the %ul makes something wrong in my computer. But using %lu solved the issue.

Actually, rather than focusing on the problem and the line of codes, I want to know about the difference between %ul and %lu. Maybe I could figure out what's wrong.

Searching doesn't give me something useful (except that "they are different").

Any explanation or link/reference is appreciated.

1 Answer 1

56

%lu is correct, while %ul is incorrect.

A printf format specifier follows the form %[flags][width][.precision][length]specifier.

u is a specifier meaning "unsigned decimal integer".

l is a length modifier meaning "long".

The length modifier should go before the conversion specifier, which means %lu is correct.

1
  • 20
    %ul is correct but won't work as the OP is expected. It'll print out the number with suffix l at the end because it's just %u, l is outside the format
    – phuclv
    May 25, 2014 at 5:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.