zoukankan      html  css  js  c++  java
  • 空暇时候思考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


  • 相关阅读:
    Nginx +keepalived
    iptables
    编译安装HTTPD 2.4.9版本
    Apache+lvs高可用+keepalive(主从+双主模型)
    LVS-HA
    corosync+pacemaker实现高可用(HA)集群
    NTP
    私人定制自己的linux小系统
    大数据处理-bitmap是个神马东西
    CSS3制作苹果风格键盘
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6853740.html
Copyright © 2011-2022 走看看