How do I printf integers? When I use:
int n = GetInt();
printf("%n \n", n);
I get the error that there was an unused variable and I can’t compile it.
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.
As the other answers indicated, you normally print an
intwith the%dconversion specification, or optionally with the%iconversion specification. You can also use%o,%xor%X(and%u), though technically there’s a signed-to-unsigned conversion implied by doing so.Note that
%nis a valid conversion specification forprintf()et al. However, unlike all the other conversion specifications, the%nconversion specification is an output operation that takes a pointer to anintand it is used to find out how many characters have been written up to that point in the format string. Therefore, you could use:Note, too, that there is almost never any need for a space before a newline.
The TR24731-1 (or ISO/IEC 9899:2011 Annex K, Bounds Checking Interfaces) defines
printf_s()et al, and explicitly outlaws the%nconversion specification because it often leads to problems precisely because it is an output parameter rather than an input parameter.