Help with strcat function

i have the code something like this:-
1
2
3
4
5
6
7
8
9
10
11
12
char buf[300];

int main()
{
char *mc_no ="002";
int a=99;
//i did something like this:-
strcat(buf,mc_no);
strcat(buf,",");
strcat(buf,a);//error here argument of type int incompatible with type const char *
return 0;
}


1
2
i want an output like this :-
002,99
try:
1
2
3
4
char a [2] = "99";
//...
strcat (buf, a);
//... 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstring>
#include <cstdio>

char buf[300];

int main()
{
   const char *mc_no = "002";
   int a = 99;

   std::strcat( buf, mc_no );
   std::strcat( buf, "," );
   std::sprintf( buf + std::strlen( buf ), "%d", a );

   return 0;
}


Take into account that the statement

std::strcat( buf, mc_no );

is correct only because buf has the static storage duration. Othewise it is better to substitute it for

std::strcpy( buf, mc_no );
Last edited on
Topic archived. No new replies allowed.