Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing an integer into a char* in C++

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?

like image 736
Galileo Avatar asked Dec 20 '09 20:12

Galileo


People also ask

Can we store integer in char in C?

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 .

Can we store integer value in 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.

How do you store integer value in character pointer?

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);


2 Answers

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

printw("%d",yournumber);
like image 123
Michael Krelin - hacker Avatar answered Oct 03 '22 15:10

Michael Krelin - hacker


The itoa function converts an int to char*.

like image 38
stiank81 Avatar answered Oct 03 '22 15:10

stiank81