为什么sizeof()在字符串中返回多一个字节?

时间:2019-07-05 04:13:17

标签: c sizeof strlen

我知道sizeof()函数返回的字节数比字符数多1个字节,因为字符串以'\ 0'字符结尾,但是当我运行sizeof('\ 0')时,它给出了4。为什么是这个吗?

#include<stdio.h>
#include<string.h>
int main()
{
    char str[]="Hello";
    int len=strlen(str);
    int size=sizeof(str);
    printf("%d %d %d",len,size,sizeof('\0'));/Gives 5 6 4
    return 0;

}

如果sizeof('\ 0')为4,那么sizeof(str)不应该给出9吗?

0 个答案:

没有答案
相关问题