Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between %ul and %lu C format specifiers?

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.

like image 926
pedyram Avatar asked May 25 '14 04:05

pedyram


People also ask

What does %lu in C mean?

%lu. Unsigned int or unsigned long. %lli or %lld.

What is difference between %D and %U in C?

%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative. If you actually want to display a pointer, use the %p format specifier.

What is the difference between %F and %G in C?

%f %f represents the data in normal decimal form, upto six decimal places, although you can control, that upto how many decimal places did you want your output. %g %g represents the decimal format of the answer, depending upon whose length is smaller, comparing between %e and %f. Also removes succeeding zeros.


1 Answers

%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.

like image 62
Jonathon Reinhart Avatar answered Oct 22 '22 13:10

Jonathon Reinhart