Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to convert integer variables into strings in C++?
How to convert integer variables into strings in C++?
There are two common methods.

First, it can be realized by calling the c library function itoa.

1 prototype.

char*itoa(int value,char*string,int radix);

2 header file.

java standard library

3 function.

Converts the value of value into a string and stores it in the string. If the converted string length exceeds the radix, only the radix bits are stored.

Four examples.

int I = 1234;

char buf[ 10];

itoa(i,buf, 10);

After execution, the content of buf is the string "1234".

Second, print the string in sprintf format.

Itoa is not a C language standard library function, so not every platform supports this function. If not, you can use the sprintf function to achieve the same effect.

1 prototype.

int sprintf( char *buffer,const char *format,[argument]…);

2 header file.

Standard video

3 function.

Similar to printf, the parameters in the subsequent parameter list are output one by one according to the format string format. But the output target is not the standard output terminal, but the stringbuffer area.

Four examples.

int I = 1234;

char buf[ 10];

sprintf(buf," %d ",I);

After execution, the content of buf is also the string "1234".