Tuesday, July 28, 2009

How to convert an 'integer' to 'string' using C language?

say there is a string and a numeric value


char str[15] = "outref"


int num = 100


i need to append this num to str - so i need to convert 'num' to string value and append it with 'str'. how is it possible?? is there any other way to do it?? please tell me.


thank you

How to convert an 'integer' to 'string' using C language?
sprintf(newstring, "%s %d", str, num);
Reply:im not sure if this works in C, but there is a function called itoa(). to use it:





char str[15] = "outref";


char charnum[10];


int num = 100;





itoa(num, charnum, 10);


strcat(str, charnum);


//now str = outref100
Reply:You can do this with sprintf().





int i = 5;


char str[10];





sprintf(str, "blah %i", i); // str is now "blah 5"


No comments:

Post a Comment