The question is why should I define size of string (string[]
should be string[some-number]
)
When the program is as following it gives me Abort tr
char buffer1[] = "computer";
Creates a buffer big enough to hold 9 characters (strlen("Hello" + 1
byte for \0
)). If you write anymore data to it what you end up with is Undefined behavior (UB). This is what happens when you do a strcat
.
UB means the program might crash or show literally any behavior. You are rather lucky that a program with UB crashes because it does not need to, but if it does atleast there is a indication of something wrong in it. Most of the times programs with UB will continue running correctly and crash when you least expect or want them to.