6

I'm writing some code that returns an integer, which then needs to be outputted using printw from the ncurses library. However, since printw only takes char*, I can't figure out how to output it.

Essentially, is there a way to store a integer into a char array, or output an integer using printw?

Galileo
  • 1,265
  • 2
  • 10
  • 9

4 Answers4

10

printw() accepts const char * as a format specifier. What you want is

printw("%d",yournumber);
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • out of curiosity, what exactly is "%d"? – Galileo Dec 20 '09 at 20:47
  • 2
    You might want to look up the printf manpage - http://linux.die.net/man/3/printf to learn the full power of formatting. in particular %d means signed integer as the first parameter after format string here. But that's not even the top of the iceberg ;-) – Michael Krelin - hacker Dec 20 '09 at 21:03
1

The itoa function converts an int to char*.

stiank81
  • 25,418
  • 43
  • 131
  • 202
0

Use itoa() or sprintf() to convert integer to ascii string.

Example:

char s[50];
sprintf(s, "%d", someInteger);

now u can pass s as char*

Pavel Radzivilovsky
  • 18,794
  • 5
  • 57
  • 67
  • 4
    Sorry, my answer is crap. Use Michael's. I just wasn't sure what ncurses printw does, so I wrote a workaround. – Pavel Radzivilovsky Dec 20 '09 at 20:37
  • 1
    You can delete answer. Several "bad" points: `itoa` -- there is no such thing in standard C (only `atoi`), it is better to use `snprintf(s,sizeof(s),"%d",someInteger)` -- safer. – Artyom Dec 20 '09 at 21:19
0

itoa will help you.

Li0liQ
  • 11,158
  • 35
  • 52