歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

C語言中sizeof()求字節數的應用舉例

本文盡可能多的測試了在Win32平台下用sizeof()求各種類型變量字節數的結果,而且會不斷更新。很有參考價值哦!

 sizeof(char)=1
 sizeof(short)=2
 sizeof(int)=4
 sizeof(long)=4
 sizeof(float)=4
 sizeof(double)=8
 sizeof(long double)=8
 
sizeof(unsigned char)=1
 sizeof(unsigned short)=2
 sizeof(unsigned int)=4
 sizeof(unsigned long)=4
 sizeof(unsigned float)=4
 sizeof(unsigned double)=4
 sizeof(unsigned long double)=8
 

char a[] = {'a','b','c','d','e'};//sizeof(a)=5
 char b[] = "abcde";//sizeof(b)=6
 char c[][3] = {{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'}};
 //sizeof(c)=12
 //sizeof(c[0])=3
 //sizeof(c[0][0])=1
 char d[][5] = {"abcc","deff","ghii","jkll"};
 //sizeof(d)=20
 //sizeof(d[0])=5
 //sizeof(d[0][0])=1
 char *p = 0;//sizeof(p)=4
 char *q = (char *)malloc(5);//sizeof(q)=4
 char *r = "abcde";//sizeof(r)=4
 char (*f)(void) = NULL;//sizeof(f)=4
 
char *s1[3] = {"aa","bb","cc"};
 //sizeof(s1)=12
 //sizeof(s1[0])=4
 //sizeof(*s1[0])=1
 
char *s2[3][3] = {{"aa","bb","cc"},{"aa","bb","cc"},{"aa","bb","cc"}};
 //sizeof(s2)=36
 //sizeof(s2[0])=12
 //sizeof(*s2[0])=4
 
char h[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
 char (*ph)[3] = h;
 //sizeof(ph)=4
//sizeof(*ph)=3
 //sizeof(ph[0])=3
 //sizeof(*ph[0])=1

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

讀C++ Primer 之構造函數陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm

讀C++ Primer 之智能指針 http://www.linuxidc.com/Linux/2011-08/40177.htm

讀C++ Primer 之句柄類 http://www.linuxidc.com/Linux/2011-08/40175.htm

將C語言梳理一下,分布在以下10個章節中:

  1. Linux-C成長之路(一):Linux下C編程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成長之路(二):基本數據類型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成長之路(三):基本IO函數操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成長之路(四):運算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成長之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成長之路(六):函數要義 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成長之路(七):數組與指針 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成長之路(八):存儲類,動態內存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成長之路(九):復合數據類型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成長之路(十):其他高級議題

Copyright © Linux教程網 All Rights Reserved