I know this doesn’t sound productive, but I’m looking for a way to remember all of the formatting codes for printf calls.
%s, %p, %f are all obvious, but I can’t understand where %d comes from. Is %i already taken by something else?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It stands for “decimal” (base 10), not “integer.” You can use
%xto print in hexadecimal (base 16), and%oto print in octal (base 8). An integer could be in any of these bases.In
printf(), you can use%ias a synonym for%d, if you prefer to indicate “integer” instead of “decimal,” but%dis generally preferred as it’s more specific.On input, using
scanf(), you can use use both%iand%das well.%imeans parse it as an integer in any base (octal, hexadecimal, or decimal, as indicated by a0or0xprefix), while%dmeans parse it as a decimal integer.Here’s an example of all of them in action:
So, you should only use
%iif you want the input base to depend on the prefix; if the input base should be fixed, you should use%d,%x, or%o. In particular, the fact that a leading0puts you in octal mode can catch you up.