• 空暇时候思考2(''等价于数字0还是字符0)


    /**********************************************************************      
    * *   Copyright (c)2015,WK Studios    
    * *   Filename:  A.h  
    * *   Compiler: GCC  vc 6.0     
    * *   Author:WK      
    * *   Time: 2015 6 7  
    * **********************************************************************/  
    #include<iostream>
    using namespace std;
    void main()
    {
    	char a[100]={'0',48,48,0,0,'0'};
    	char b[]={'0',48,48,0,0,'0'};
    	char c[]={'0','0'};
    	char d[]={0};
    	//注意一下数字0与字符'0'差别
    	//''等价于数字0而不是字符0
    	cout<<sizeof(a)<<endl;
    	cout<<strlen(a)<<endl;
    	cout<<sizeof(b)<<endl;
    	cout<<strlen(b)<<endl;
    	cout<<sizeof(c)<<endl;
    	cout<<strlen(c)<<endl;
    	cout<<sizeof(d)<<endl;
    	cout<<strlen(d)<<endl;
    

    执行结果:

    100

    3

    6

    3

    2

    7

    1

    0

    不行的话再看一个:

    #include <iostream>
    using namespace std;
    
    
    void example()
    {
    	int i;
    	char acNew[20];
    	for(i = 0; i < 5; i++)
    	{
    		acNew[i] = '0' ;
    	}
    	printf("%d
    ",strlen(acNew));
    	return ;
    }
    
    void main()
    {
    	example();	
    }
    结果是一个随机的值,由于strlen没有找到结束的表示符‘’

    略微修改一下:

    #include <iostream>
    using namespace std;
    
    
    void example()
    {
    	int i;
    	char acNew[20];
    	for(i = 0; i < 5; i++)
    	{
    		acNew[i] = 0 ; // '1'  0
    	}
    	printf("%d
    ",strlen(acNew));
    	return ;
    }
    
    void main()
    {
    	example();	
    }

    这次结果是0


  • 相关阅读:
    vue中使用vuepdf插件显示pdf
    vuecirclemenu漂亮的圆形菜单
    console.log输出彩色字,图片等
    项目开发之使用 maven
    用 C 扩展 python
    Flash 与 php 使用 amfphp
    纯命令行的编辑利器:用好 awk 与 sed
    远程调试Java程序
    .net core 项目文件结构浅析
    初识Redis系列之一:简单介绍
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6853740.html
Copyright © 2020-2023  润新知