Help with strcat function
Sep 20, 2012 at 7:54am UTC
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
Sep 20, 2012 at 8:03am UTC
try:
1 2 3 4
char a [2] = "99" ;
//...
strcat (buf, a);
//...
Sep 20, 2012 at 9:04am UTC
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 Sep 20, 2012 at 1:43pm UTC
Topic archived. No new replies allowed.