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?
C requires int be at least as many bits as char . Therefore, int can store the same values as char (allowing for signed/unsigned differences). In most cases, int is a lot larger than char .
You can not store an integer value in a char type as it. It will be converted to corresponding ASCII character and will be treated as a character.
Heap allocation char *str = malloc(10); sprintf(str1,"%d",10); ... Show activity on this post. char * str1 = malloc(sizeof(char)*10); //#include <stdlib. h> for malloc to allocate memory for keeping the chars sprintf(str1,"%d",10);
printw()
accepts const char *
as a format specifier. What you want is
printw("%d",yournumber);
The itoa function converts an int to char*.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With