警告
warning: ‘str…’ directive output truncated writing 47 bytes into a region of size between 34 and 44 [-Wformat-truncation=]
原因
这个警告的原因,如果定义的字符串数组是固定大小,那么编译器会在编译阶段进行计算。看写到数组里的字符串是否超过数组的本身大小。如果超了,就报警告。
类似的警告:
类似
strncpy.c:9:10: warning: passing argument 1 to restrict-qualified parameter aliases with argument 3 [-Wrestrict]
  sprintf(abc,"abcedfeag%s\n", abc);
          ^~~                  ~~~
strncpy.c:9:24: warning: ‘%s’ directive writing up to 9 bytes into a region of size 1 [-Wformat-overflow=]
  sprintf(abc,"abcedfeag%s\n", abc);
                        ^~     ~~~
strncpy.c:9:2: note: ‘sprintf’ output between 11 and 20 bytes into a destination of size 10
  sprintf(abc,"abcedfeag%s\n", abc);
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


