-1

How can an integer be converted into a string in C? Suppose we have an int array:

int a[4]={21,1212,53,4131};

and each int is to be converted in to strings: "21","1212","53","4131".

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

0

Basically use itoa or sprintf and/or read the many great detailed answer that were already posted.

Community
  • 1
  • 1
Lilley
  • 214
  • 1
  • 5
0

Use sprintf or snprintf:

char buf[4][5];
for(int i=0; i<4; i++)
  sprintf(buf[i],"%d",a[i]);

Or

char buf[4][5];
for(int i=0; i<4; i++)
  snprintf(buf[i],sizeof(buf[i]),"%d",a[i]);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83