String is longer than expected in C

前端 未结 3 940
轮回少年
轮回少年 2020-12-12 02:48

Here\'s my code

#include 
#include 

int main(){
    char pal[8] = \"ciaooaic\";
    char pal1[7] = \"ciaoaic\";
    int lenPa         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 03:30

    You have not kept space for the NULL terminating character \0.


    Either increase the size of the array by 1

    char pal[9] = "ciaooaic";
    char pal1[8] = "ciaoaic";
    

    OR

    Do not specify the length at all

    char pal[] = "ciaooaic";
    char pal1[] = "ciaoaic";
    

提交回复
热议问题