I'm writing a similar program on C on Ubuntu. The task is to show the output on console. I am using gcc compiler. The task is to get such an output 12, Name Surname, 36, my lapt, -11, but instead i get 12, apt, 36, my lapt, -11. I know that my program is overwriting a at strncat(d, c, 4);, but i don't know the reason why.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[] = "Name";
char b[] = "Surname";
strcat(a, b);
int x = strlen(a);
int y = strcmp(b, a);
char d[] = "my ";
char c[] = "laptop";
strncat(d, c, 4);
int z = strncmp(a, c, 2);
printf("%d, %s, %d, %s, %d\n", x, a, y, d, z);
return 0;
}
aonly has space to store 5 bytes, including string terminator. You can'tstrcatanything into it - you'll get undefined behavior.