/* 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';