本文列举了sizeof与strlen的区别,总结一下:
sizeof()的结果是一个对象占用的字节数,对于字符串包括字符串结尾的\0.
strlen()的结果不包括\0.
下面是源码:
#include <stdio.h> int main() { int i = 0; char acNum[] = "12345"; ; char ac3Num[3] = "12"; char *pcNum = "12345" //printf("sizeof(int)=%d \r\n", sizeof(int)); printf("sizeof(array)=%d \r\n", sizeof(acNum)); printf("strlen(array)=%d \r\n", strlen(acNum)); printf("sizeof(array3)=%d \r\n", sizeof(ac3Num)); printf("strlen(array3)=%d \r\n", strlen(ac3Num)); printf("sizeof(ptr)=%d \r\n", sizeof(pcNum)); printf("strlen(ptr)=%d \r\n", strlen(pcNum)); return 0; }
运行结果如下:
时间: 2024-12-24 04:17:39