what's the difference between NULL 0 '\0' and ""/''(space string or char) ?



/* NULL_ZERO.c by vinco at 2011-09-01
* what's the difference between NULL 0 '\0' and ""/''(space string or char)
*/

#include<stdio.h>
#include<string.h>

#define ZERO 0

char * utostr(int num,int base,char* str);

int main()
{
	char str[64];
	// firstly, the code below tell you that NULL, 0, '\0' are equivalent to each other
	if(NULL == ZERO && NULL == '\0' )
		printf("NULL == ZERO == '\\0' \n");
	else 
		printf("NULL != ZERO != '\\0' \n");

	printf("\n");
	printf("print NULL as %%d = %d\n", NULL);
	printf("print NULL as %%p = %p\n", NULL);
	printf("print NULL as %%s = %s\n", NULL);
	printf("\n");
	printf("print 0 as %%d= %d\n",0);
	printf("print 0 as %%p = %p\n",0);
	printf("print 0 as %%s = %s\n",0);
	printf("\n");
	printf("print '\\0' as %%d = %d\n", '\0');
	printf("print '\\0' as %%p = %p\n", '\0');
	printf("print '\\0' as %%s = %s\n", '\0');
	printf("\n");
	
	//secondly ""(space string) is merely/simply a const which is a string type
	printf("print \"\" as %%d = %d\n", ""); //the Decimal address of the space string("")
	printf("print \"\" as %%p = %p\n", ""); //the Hexadecimal address of the space string("")
	printf("print \"\" as %%s = %s\n", ""); //the value of the space string("")
	printf("\n");
	
	memset(str, 0, sizeof(str));
	printf("cast the address of \"\" from Decimal to Hexadecimal is %d = 0x%s\n","", utostr("", 16, str));

	//below code cannot pass the compile
	//printf("print '' as %%d = %d\n", '');
	//printf("print '' as %%p = %p\n", '');
	//printf("print '' as %%s = %s\n", '');
	printf("\n");
	
	return(0);
	
}

char * utostr(int num,int base,char* str)
{
	int tmp = num,i = 0;
	int len = 0;
	char* p = str;
	char t = 0;

	while(tmp)
	{
		i = tmp%base;
		*p++ = i + '0';//toChar(i);
		tmp = tmp/base;
	}
	*p++ = '\0';

	//strrev(str);

	len = strlen(str) - 1;
	for(i=0,p=str ; i<len ; i++, len--)
	{
		t = *(p+i) ;
		*(p+i) =  *(p+len) ;
		*(p+len) = t ;
	}

	return str;
}



compile and run it in Ubuntu ( CC/GCC-4.4.1 ) as follow:

root@vinco:/home/vinco/c# make NULL_ZERO
cc     NULL_ZERO.c   -o NULL_ZERO
NULL_ZERO.c: In function ‘main’:
NULL_ZERO.c:22: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’
NULL_ZERO.c:24: warning: reading through null pointer (argument 2)
NULL_ZERO.c:24: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘void *’
NULL_ZERO.c:27: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int’
NULL_ZERO.c:28: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
NULL_ZERO.c:31: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘int’
NULL_ZERO.c:32: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
NULL_ZERO.c:36: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char *’
NULL_ZERO.c:42: warning: passing argument 1 of ‘utostr’ makes integer from pointer without a cast
NULL_ZERO.c:10: note: expected ‘int’ but argument is of type ‘char *’
NULL_ZERO.c:42: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char *’
root@vinco:/home/vinco/c# ./NULL_ZERO
NULL == ZERO == '\0' 

print NULL as %d = 0
print NULL as %p = (nil)
print NULL as %s = (null)

print 0 as %d= 0
print 0 as %p = (nil)
print 0 as %s = (null)

print '\0' as %d = 0
print '\0' as %p = (nil)
print '\0' as %s = (null)

print "" as %d = 134515050
print "" as %p = 0x804896a
print "" as %s = 

cast the address of "" from Decimal to Hexadecimal is 134515050 = 0x804896:

root@vinco:/home/vinco/c#

sorry!, it seem that there are some implict bugs in the function "utostr()", the result should be 0x804896a, but not 0x804896: !!!


Just as the comment displayed:

1. NULL, 0, '\0' are equivalent to each other;

2. ""(space string) is merely/simply a const which is a string type

3. it doesn't make any sense if you want to compare ""(empty string constant), ''(empty character constant), or '0' "0" with 0/NULL/'\0';



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值