Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3904023
  • 博文数量: 534
  • 博客积分: 10470
  • 博客等级: 上将
  • 技术积分: 4800
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-26 14:08
文章分类

全部博文(534)

文章存档

2021年(1)

2019年(1)

2017年(1)

2016年(2)

2013年(2)

2012年(10)

2011年(43)

2010年(10)

2009年(17)

2008年(121)

2007年(253)

2006年(73)

分类:

2007-08-27 11:08:41

写了些容易出错的小测试:

1>. sizeof and strlen

Code: test1.c
#include
#include
                                                                                
void func(char *str)
{
  printf("func() sizeof: %d \n", sizeof(str));
  printf("func() strlen: %d \n", strlen(str));
}
                                                                               
int main(int argc, char **argv)
{
  char a[] = "123456789";
                                                                               
  printf("main() sizeof: %d\n", sizeof(a));
                                                                               
  func(a);
                                                                               
  return (0);
}

Environment:
$ uname -a
Linux localhost 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

Make:
$ gcc -Wall -o test1 test1.c

Result:
$./test1
main() sizeof: 10
func() sizeof: 4       
func() strlen: 9

注意:
    sizeof是C语言的一种单目操作符,如C语言的其他操作符++、--等。它并不是函数。sizeof操作符以字节形式给出了其操作数的存储大小。操作数可以是一个表达式或括在括号内的类型名。操作数的存储大小由操作数的类型决定。
    1>. func中的sizeof(str)其实返回的是: str这个字符指针的占用的内存大小.
    2>. strlen得出来的字符长度是不包含'\0'字符的实际长度.
    3>. main 中的sizeof为整个字符实际占用的内存大小.   
   
    紧记一点:sizeof求的是类型的字节数,它只跟变量声明的类型有关,其他就“免谈”了。
   
-----------------------
2>. char and unsigned char

Code: test2.c
#include
                                                                               
int main(int argc, char **argv)
{
  int             t;
  char            c1;
  unsigned char   c2;
                                                                               
  t   = 0x6F;
  c1  = t;  c2  = t;
                                                                               
  printf("t=0x%02X c1=0x%02X c2=0x%02X \n"
    , t, c1, c2);
                                                                               
  t   = 0xEF;
  c1  = t;  c2  = t;
                                                                               
  printf("t=0x%02X c1=0x%02X c2=0x%02X \n"
    , t, c1, c2);
                                                                               
  return (0);
}

Environment:
$ uname -a
Linux localhost 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

Make:
$ gcc -Wall -o test2 test2.c

Result:
$ ./test2
t=0x6F c1=0x6F c2=0x6F
t=0xEF c1=0xFFFFFFEF c2=0xEF

注意:
    char类型的在保存数据时只可以保存到0 ~ 0x7F之间的数据, 最高位为0. 不然得出来的数据你会很吃惊的.

-------------------

3>. int and unsigned int

Code: test3.c
#include
                                                                               
int main(int argc, char **argv)
{
  int           t1 = 6;
  unsigned int  t2 = -12;
                                                                               
  (t1 + t2 > 6) ? puts("> 6") : puts("<= 6");
                                                                               
  return (0);
}

Environment:
$ uname -a
Linux localhost 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

Make:
$ gcc -Wall -o test3 test3.c

Result:
$ ./test3
> 6

注意:
    当表达式中存在有符号类型和无符号类型时所有的操作数都自动转换为无符号类型。
   
-------------------------

4>. malloc function

Code: test4.c
#include
#include
                                                                               
int main(int argc, char **argv)
{
  char *ptr;
                                                                               
  if ((ptr = (char *)malloc(0)) == NULL)
    puts("Got a null pointer");
  else
    puts("Got a valid pointer");
                                                                               
  return (0);
}

Environment:
$ uname -a
Linux localhost 2.4.20-8 #1 Thu Mar 13 17:54:28 EST 2003 i686 i686 i386 GNU/Linux

Make:
$ gcc -Wall -o test4 test4.c

Result:
$ ./test4
Got a valid pointer

注意:
    库函数malloc参数为0返回的指针也是合法的.  

----------------------

以后有什么好的再逐渐添加上来... 
阅读(2698) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~