C - Appending an int to a string

Viewed 69

I would like to append the integer n to the string a, resulting in "asd 2". For some reason I cannot figure out how to do it, I have tried casting int to char and using strcat(), I tried it with sprintf() yet none of it works.

char* a = "asd ";
int n = 2;

Surely there has to be a simple way to do this? I would appreciate any help

1 Answers

sprintf solution

char result[100];
char* a = "asd";
int n = 2;
sprintf(result, "%s %d", a, n);
Related