码迷,mamicode.com
首页 > 其他好文 > 详细

sizeof 和 strlen

时间:2018-07-22 23:37:42      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:初始   ide   hello   指针   gif   color   源码   []   onclick   

综述

strlen()求的是长度,针对的对象是字符串.

sizeof()求的是大小,针对的是类型。

首先要明确的一个概念是,strlen()是函数,而sizeof()表面看起来是函数,其本质是关键字。

函数必须有()

我们在写代码的时候,对于没有参数的函数,再调用的时候还是要写成fun(),为啥必须带那个()呢?反正有没有参数,省掉()岂不是更好。

函数名本质上代表函数的地址,对于没有参数的函数,如果不写(),也是可以编译过的,只不过不能调用函数。

不带()源码:

技术分享图片
 1 #include <stdio.h>
 2 void fun()
 3 {
 4     printf("hello\n");
 5 }
 6 
 7 void main()
 8 {
 9     fun;
10     getchar();
11 }
View Code

运行结果:

技术分享图片

带()源码:

技术分享图片
 1 #include <stdio.h>
 2 void fun()
 3 {
 4     printf("hello\n");
 5 }
 6 
 7 void main()
 8 {
 9     fun();
10     getchar();
11 }
View Code

技术分享图片

以上实验说明,函数必须有()。

证明sizeof是个关键字

sizeof是个关键字,有没有()都可以。但是为了程序可读性,最好加上()

证明sizeof是个关键字,源码

技术分享图片
 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     int a = 10;
 6     printf("%d\n",sizeof(a));
 7     printf("%d\n", sizeof a);
 8     printf("%d\n", sizeof(10));
 9     printf("%d\n", sizeof 10);
10     printf("%d\n", sizeof(int));
11     //printf("%d\n", sizeof int);错误写法
12     getchar();
13 }
View Code

运行结果

技术分享图片

区分数字0,字符0,字符‘\0‘

字符‘0‘和字符‘\0‘是完全不一样的东西,‘\0‘是字符串的终结者。但是字符‘\0‘和数字0是等价的。见代码

技术分享图片
 1 #include <stdio.h>
 2 
 3 void main()
 4 {
 5     /* 输出值5
 6     char str[10] = { ‘h‘,‘e‘,‘0‘,‘l‘, ‘o‘ };
 7     printf("%d\n", strlen(str));*/
 8     /*输出值2
 9     char str[10] = { ‘h‘,‘e‘,‘\0‘,‘l‘, ‘o‘ };
10     printf("%d\n", strlen(str));*/
11     /*输出值2
12     char str[10] = { ‘h‘,‘e‘,0,‘l‘, ‘o‘ };
13     printf("%d\n", strlen(str));*/
14     getchar();
15 }
View Code

 

技术分享图片
 1 #include <stdio.h>
 2 
 3 //void main()
 4 //{
 5 //    char str[]="hello";
 6 //    printf("%d\n",sizeof(str));//6
 7 //    printf("%d\n", strlen(str));//5
 8 //    getchar();
 9 //}
10 
11 //void main()
12 //{
13 //    char str[10] = "hello";
14 //    printf("%d\n", sizeof(str));//10
15 //    printf("%d\n", strlen(str));//5
16 //    getchar();
17 //}
18 
19 //void main()
20 //{
21 //    char str[] = {‘h‘,‘e‘,‘l‘,‘l‘, ‘o‘};
22 //    printf("%d\n", sizeof(str));//5
23 //    printf("%d\n", strlen(str));//不一定,看啥时候早到\0了。strlen的关键就在于寻早‘\0‘
24 //    getchar();
25 //}
26 
27 //void main()
28 //{
29 //    char str[10] = { ‘h‘,‘e‘,‘l‘,‘l‘, ‘o‘ };
30 //    printf("%d\n", sizeof(str));//10
31 //    printf("%d\n", strlen(str));//5 获益于不完全初始化,字母o后面被填充了数字0
32 //    getchar();
33 //}
34 
35 //void main()
36 //{
37 //    char str[10] ;
38 //    printf("%d\n", sizeof(str));//10
39 //    printf("%d\n", strlen(str));//不一定,看啥时候早到\0了。strlen的关键就在于寻早‘\0‘
40 //    getchar();
41 //}
42 
43 //char str[10];
44 //void main()
45 //{
46 //    printf("%d\n", sizeof(str));//10
47 //    printf("%d\n", strlen(str));//0
48 //    getchar();
49 //}
50 //当str定义为全局变量的时候,str在栈区。相比较局部变量,在未初始化前提下,系统给他们的初始值不一样。随机变量的初始值是随机值。栈区用数字零填充
51 
52 //void main()
53 //{
54 //    char *str="Hello" ;
55 //    printf("%d\n", sizeof(str));//4 str是一个指针类型,sizeof是求类型大小
56 //    printf("%d\n", strlen(str));//5
57 //    getchar();
58 //}
59 
60 void main()
61 {
62     char *str[3] = {"Hello","My","UESTC"};
63     printf("%d\n", sizeof(str));//
64     printf("%d\n", sizeof(*str));//
65     printf("%d\n", strlen(str));
66     printf("%d\n", strlen(*str));//
67 
68     getchar();
69 }
View Code

 

sizeof 和 strlen

标签:初始   ide   hello   指针   gif   color   源码   []   onclick   

原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9351697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!