C零基础视频-29-对于字符串的sizeof与strlen的区别

[TOC]

#sizeof与strlen的不同表现 看程序说结果:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[])
{
    char* szHello = "Hello";
    printf("%d\r\n", sizeof(szHello));
    printf("%d\r\n", sizeof("Hello"));
    printf("%d\r\n", strlen(szHello));
    return 0;
}

#关于sizeof与strlen的对比总结

  • sizeof是运算符,编译时求各种变量、类型大小,对字符串会包括结尾的'\0'
  • strlen是函数,求字符串的长度,不包括结尾的'\0'

猜你喜欢

转载自www.cnblogs.com/shellmad/p/11695568.html