欢迎来到贱贱的博客

扩大
缩小

c语言sizeof与strlen的区别

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 //strlen与sizeof的区别
 6 //strlen() 是函数 参数是字符串 并且必须一串结束符\0结尾
 7 //sizeof是关键字 返回的是参数所占的内存数  在学习数据类型的时候有很大的帮助
 8 void main()
 9 {
10     char str[5] = "love";//此处数组为什么会是5或者更大 是因为有默认的\0
11     char str2[4]={'a','b','c','\0'};
12 
13     //printf("字符串的长度为:%d\n",strlen(str));//4 没有算\0
14     //printf("所占的内存大小为:%d\n",sizeof(str));//8
15 
16     system("pause");
17 }

 

posted on 2016-11-07 14:38  L的存在  阅读(256)  评论(0编辑  收藏  举报

导航