main()
{
char a,b
a='A';
b='B';
strcat(b,a);
printf("a:%s b:%s",a,b);
}
views:
56answers:
2
+1
A:
You can start by learning basic C (especially pointers, arrays, and managing memory with malloc).
gnucom
2010-06-08 05:25:17
+1
A:
The char
type only holds a single character. To hold a string, you need an array of characters. In C, arrays are fixed in size, so the programmer must ensure that a character array has enough space to hold the string you want to put in it.
#include <stdio.h> //for printf
#include <string.h> //for strncat
#define A_SIZE 10
int main(void)
{
char a[A_SIZE] = "A";
char b[] = "B";
strncat(a, b, A_SIZE - strlen(a) - 1);
printf("a:%s b:%s\n", a, b);
}
Artelius
2010-06-08 05:26:18
thank you sir..
thulasi
2010-06-08 05:39:55
The last parameter of `strncat()` is the *number of characters to copy* (not including the terminating `\0`). So it should be `A_SIZE - strlen(a) - 1`.
caf
2010-06-08 05:45:01
You're right, fixed it. But strncat seems inconsistent with the other 'n' functions...
Artelius
2010-06-08 06:47:10