i have two translation units..
file1.c
#include<stdio.h>
#include<string.h>
extern char a[90];
int main(int argc,char ** argv){
//printf("%s ",a);
strcat(a,"y Code");
printf("%s",a);
return 0;
}
file2.c
char a[4]={'Q','u','i','r'};
by compilation and linking of project give no error but when i exceute the program it gives segmentation fault error(in Debug mode). i think that some how the linker resolves wrong link for identifier a in file1.c to definition a in file2.c, this happens when i also change the data type of a in file2.c(definition of a) how it is possible, what mechanism the linker uses to link identifiers in same or different translation units?
You are getting a segmentation fault because you have defined
char a[4]
and you haveextern char a[90]
You also need to enable warnings using
-Wall
, during linkage step compiler recognizes this variable, thus you get no error.