-3

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.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
penguinshin
  • 53
  • 1
  • 1
  • 3
  • I wonder if this function is documented anywhere on the internet... nah, probably not. – Ed S. Aug 07 '12 at 00:19
  • Just out of curiosity, why did you expect `"%n"` to work? A tip: If you're writing a call to a library function, *always* read the documentation for that function unless you're absolutely certain you know what you're doing. (And the space before the `\n` is probably unnecessary.) – Keith Thompson Aug 07 '12 at 00:21
  • @KeithThompson: I'm guessing it was a perceived association with the name of the variable... or a guess, and the OP chose "n" for "number". I don't know... – Ed S. Aug 07 '12 at 00:23
  • Oh, and the code you've shown us shouldn't produce an "unused variable" warning, nor should such a warning prevent you from compiling the program (unless you're using something like `gcc -Werror`). You've got problems elsewhere in your code. – Keith Thompson Aug 07 '12 at 00:23
  • @EdS.: No offense, but what's the point of guessing how the OP might answer my question? – Keith Thompson Aug 07 '12 at 00:23
  • @KeithThompson: ...I don't know... I think I'm just bored. I should move on. – Ed S. Aug 07 '12 at 00:26
  • Thanks for the help...I have no idea where to look for any of this, so I ask for help. And I just started learning programing and c language for that matter, today. – penguinshin Aug 07 '12 at 00:29
  • Stack Overflow is not a replacement for your system's man pages. – tbert Aug 07 '12 at 02:38
  • I'd like to explain why I was confused. The I didn't understand that the "%s" or "%d" wasn't the variable itself but rather a way to display a variable (perhaps of different type) specified later in the syntax. – penguinshin Aug 15 '12 at 04:12

4 Answers4

1

A signed integer uses %d (or %i).

See also man 3 printf (on Unix-like systems) for the whole list of modifiers.

Kevin Grant
  • 5,363
  • 1
  • 21
  • 24
0

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.

Here's a more in depth explanation of all the format codes

timrau
  • 22,578
  • 4
  • 51
  • 64
Antimony
  • 37,781
  • 10
  • 100
  • 107
0

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.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

int n;

printf("get number:");
scanf("%d",&n);
scanf("%d",&n);

printf("your number is %d\n",n); return 0;

S.G
  • 129
  • 11