how to use itoa() function

Hi Experts,

I was using
#include <iostream.h>
#include <stdlib.h>

void main()
{

char *charValue = "123";
int value;
value = atoi(charValue);
cout << value << endl;
value = 256;

char buff[5];
itoa( value, buff, 10 );         <<<<-----

cout << buff << endl;

}

I am getting the error "itoa" unknown identifier.  But, I included the correct header file, I guess..  So, what is wrong.... I am running it in Linux. using g++....
LVL 1
thaneshAsked:
Who is Participating?
 
jkrCommented:
There is no 'itoa()' on Linux, '_itoa()' is a convenience function for MS compilers. Just use

#include <iostream> // NO .h!
#include <stdlib.h>
#include <stdio.h>

using namespace std;

void main()
{

char *charValue = "123";
int value;
value = atoi(charValue);
cout << value << endl;
value = 256;

char buff[5];
sprintf( buff,"%d", value);  
cout << buff << endl;

}
0
 
e_tadeuCommented:
itoa is not ANSI-C
Use sscanf (in C) or an istringstream (in C++) instead
0
 
e_tadeuCommented:
Sorry, use sprintf or ostringstream instead!

#include <sstream>

void main() {
  std::ostringstream oss;
  int value = 256;
  oss << value;

  cout << oss.str() << endl;
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.