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.
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.
A signed integer uses %d
(or %i
).
See also man 3 printf
(on Unix-like systems) for the whole list of modifiers.
That's because you need to use a format specifier corresponding to the integer you passed. The most common is %d
. Try replacing the %n
with a %d
.
As the other answers indicated, you normally print an int
with the %d
conversion specification, or optionally with the %i
conversion specification. You can also use %o
, %x
or %X
(and %u
), though technically there's a signed-to-unsigned conversion implied by doing so.
Note that %n
is a valid conversion specification for printf()
et al. However, unlike all the other conversion specifications, the %n
conversion specification is an output operation that takes a pointer to an int
and it is used to find out how many characters have been written up to that point in the format string. Therefore, you could use:
int n = GetInt();
int c;
printf("%d%n\n", n, &c);
printf("%d characters in number %d\n", c, n);
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 %n
conversion specification because it often leads to problems precisely because it is an output parameter rather than an input parameter.
int n;
printf("get number:");
scanf("%d",&n);
scanf("%d",&n);
printf("your number is %d\n",n); return 0;