How should character arrays be used as strings?
问题 I understand that strings in C are just character arrays. So I tried the following code, but it gives strange results, such as garbage output or program crashes: #include <stdio.h> int main (void) { char str [5] = "hello"; puts(str); } Why doesn't this work? It compiles cleanly with gcc -std=c17 -pedantic-errors -Wall -Wextra . Note: This post is meant to be used as a canonical FAQ for problems stemming from a failure to allocate room for a NUL terminator when declaring a string. 回答1: A C