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?

4 Answers 4

10

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

printw("%d",yournumber); 
Sign up to request clarification or add additional context in comments.

2 Comments

out of curiosity, what exactly is "%d"?
You might want to look up the printf manpage - 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 ;-)
1

The itoa function converts an int to char*.

Comments

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*

2 Comments

Sorry, my answer is crap. Use Michael's. I just wasn't sure what ncurses printw does, so I wrote a workaround.
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.
0

itoa will help you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.