I'm currently learning to program in C and I can't understand why my terminal outputs: "zsh: trace trap ./a.out" when running my code.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello ";
char str2[] = "Wolrd!";
strcat(str, str2);
printf("%s\n", str);
return 0;
}
This is what I type in the terminal:
gcc test.c
./a.out
CodePudding user response:
The problem is str
is allocated based on the size of the literal you are initialing it with. strcat
presumes you have enough room to concatenate both strings.
change str[]
to str[100]
and it would work because you have sufficient space for both strings. This fixes your problem but you need to understand arrays, memory allocation and read the expectations of the functions like strcat
.
CodePudding user response:
The backticks (the code inside the backticks) makes a command substitution that will find the first command named bash in the $PATH and run that instead of bash. There is no error in zsh, because zsh is a shell for interactive use and doesn't need a compilation, therefore zsh is not interested in the -c (the compiler option) of the gcc compiler.