The difference between 0 and '0' in array

前端 未结 2 1537
情书的邮戳
情书的邮戳 2020-12-18 13:12

I have a question about array initialization

What is the difference between

char a[6]={0};

and

char a[6]={\'0\',\'0\',\'0\',\'0\',\'0\',\'0\

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-18 13:49

    '0' is the ASCII character for the number 0. Its value is 48.

    The constant 0 is a zero byte or null byte, also written '\0'.

    These four are equivalent:

    char a[6] = {0};
    char a[6] = {0, 0, 0, 0, 0, 0};
    char a[6] = {'\0', '\0', '\0', '\0', '\0', '\0'};
    char a[6] = "\0\0\0\0\0"; // sixth null byte added automatically by the compiler
    

提交回复
热议问题