strlen()和sizeof()

C语言求字符串长度

strlen():返回的是直接可观察到的字符数量
sizeof():返回的是实际内存空间,默认情况下,也计算结束符’\0’

#include <stdio.h>
#include <string.h>
int main(){
    
    
	char str[] = "hello world!";
	printf("%d\n" ,sizeof(str)); // 13 多了'\0 '  
	printf("%d\n" ,strlen(str)); // 12
	
	char str1[20] = "hello world!";
	printf("%d\n" ,sizeof(str1)); // 20 
	printf("%d\n" ,strlen(str1)); // 12
}

猜你喜欢

转载自blog.csdn.net/weixin_43865875/article/details/108187971