Why does the code fail with a segmentation fault for the first set of code but the second block doesn't? (Only difference is that the chars are static in the first but not static in the second).
#include <string.h>
#include <stdio.h>
static char a[16];
static char b[16];
static char c[32];
int main(int argc, char *argv[]) {
strcpy(a, "0123456789abcdef");
strcpy(b, "0123456789abcdef");
strcpy(c, a);
strcat(c, b);
printf("a = %s\n", a);
return 0;
}
.
#include <string.h>
#include <stdio.h>
char a[16];
char b[16];
char c[32];
int main(int argc, char *argv[]) {
strcpy(a, "0123456789abcdef");
strcpy(b, "0123456789abcdef");
strcpy(c, a);
strcat(c, b);
printf("a = %s\n", a);
return 0;
}
At first I thought it's because of where they are stored but they are both in the bss region (both global and uninitialized). From what I understood and read here on Stackoverflow, all static does is make the variable limited to an internal linkage but nothing else.
(I know that there is no space allocated for the null character. This behavior is consistent).