<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          C語言各數(shù)據(jù)類型的內(nèi)存映像

          共 5362字,需瀏覽 11分鐘

           ·

          2022-06-20 11:18

          C語言各種數(shù)據(jù)類型的內(nèi)存映像(32位平臺):


          0 signed char

          #include <stdio.h>int main(){    char min = 1<<7;    char max = (1<<7)-1;    for(int i=min;i<=max;i++)        if(i<0)            printf("%.2X ",(unsigned char)i);        else
          { printf("%c ",i); if(i%32==0) printf("\n%d ",i);
          }
          getchar();
          }

          output:


          1 整型的signed和unsigned

          #include <stdio.h>int main(){    signed int smin = 1<<31;    signed int smax = (1<<31)-1;    printf("%d\n",smin);    // -2147483648
          printf("%d\n",smax); // 2147483647
          unsigned int umax = -1; printf("%u\n",umax); // 4294967295
          umax = (1<<32)-1; printf("%u\n",umax); // 4294967295}

          如果一個表達式同時包含signed和unsigned整型,signed會提升為unsgined,可能會隱藏一些意想不到的錯誤,特別是用在比較運算時:

              unsigned int a=4294967290;    int b=-6; 
          printf("%d\n",a==b); // 1 , b promotes to unsigned

          2 double的二進制位顯示

          #include <stdio.h>void printByte(double d){    int bs = sizeof d;    unsigned char *ch = (unsigned char*)&d;    for(int i=0;i<bs;i++)        printf("%.2X ",*(ch+i));
          }int main(){ int n = 0x01020304; if(*(char*)&n == 4) printf("小端:");//小端:
          double d = 15.75; // 1111.11, 指數(shù)位值:1023+3
          //0 100 0000 0010 1111100000000000000000000000000000000000000000000000
          printByte(d);//00 00 00 00 00 80 2F 40
          // 40 2F 80
          // 0100 0000 0010 1111 1000 0000
          getchar();
          }

          將double分成4部分顯示:

          #include <stdio.h>typedef struct packed_double {
          unsigned int low32; // 小數(shù)位 低32位
          unsigned int low20:20; // 小數(shù)位 低33-52位
          unsigned int exp11:11; // 指數(shù)位 低53-63位,移碼1023+二進制整數(shù)位-1
          unsigned int sign:1; // 符號位} packed_double;typedef union { double d;
          packed_double b;
          } packed;int main(){
          packed pd;
          pd.d = -15.75;
          pd.d = 12.3; printf("%u %u %u %u\n",pd.b.sign,pd.b.exp11,pd.b.low20,pd.b.low32);
          getchar();
          return 0;
          }/*
          0 1026 1015808 0
          */

          3 數(shù)組是相同數(shù)據(jù)類型的依次存儲

          數(shù)組名是一個存儲數(shù)據(jù)首元素地址具有常量性質(zhì)的特殊指針,成員是相對于基址的偏移:

          #include <stdio.h>void printArr(short arr[],int len){    for(int i=0;i<len;i++)
          { printf("%d ",*(arr+i));
          } printf("\n");
          }int main(){
          short arr[] = {1,3,2}; int len = sizeof arr / sizeof *arr;
          printArr(arr,len);
          }

          4 枚舉只是枚舉可以取值的一些符號常量的一個特殊整型

          #include <stdio.h>int main(){    enum Nm{LOSS,TIE,WIN}nm; // 實質(zhì)是一個整型,成員只是可能的右值(符號常量)的枚舉
          nm = LOSS; printf("%d ",nm); // 0
          nm = TIE; printf("%d ",nm); // 1
          nm = WIN; printf("%d ",nm); // 2
          nm = (enum Nm)3;
          printf("%d ",nm); // 3
          printf("\n%d",sizeof(enum Nm)); // 4
          getchar();
          }

          枚舉讓相關符號常量內(nèi)聚為一組,相對于#define,枚舉對數(shù)據(jù)的描述性更清晰。

          5 共用體成員的起始地址相同,共用一塊內(nèi)存空間,值相互覆蓋

          #include <stdio.h>int main(){    union Nn{int a; double b;}nn;// 成員的起始地址相同,值相互覆蓋
          nn.a = 123; //
          printf("起始地址:%X,內(nèi)存空間占用:%d\n",&nn.a,sizeof nn.a);
          nn.b = 12.3; printf("起始地址:%X,內(nèi)存空間占用:%d\n",&nn.a,sizeof nn.b);
          nn.a = 12; printf("起始地址:%X,內(nèi)存空間占用:%d\n",&nn.a,sizeof nn.a);
          getchar();
          }/*
          起始地址:12FF40,內(nèi)存空間占用:4
          起始地址:12FF40,內(nèi)存空間占用:8
          起始地址:12FF40,內(nèi)存空間占用:4
          */

          當一些事物具有更多共性,但有少量差異時,可以只用一個內(nèi)嵌一個共用體的結構體來描述:

          #include <stdio.h>#include <string.h>#define MAXPARTS 12struct Parts{ // 零件
          int cost;char supplier[12];char unit[12] ;
          };struct Assembly{ // 裝配件
          int n_parts;struct {
          char partno[12];
          short quan;
          }parts[MAXPARTS];
          };struct Inventory{ // 存貨類型,或是零件,或是裝配件
          char partno[10];int quan;enum{PART,ASSEMBLY}type; // 存貨類型
          union {struct Parts parts;
          struct Assembly assembly;
          }info;
          };int main(){struct Inventory screen;
          strcpy(screen.partno,"p001");
          screen.quan = 12;
          screen.type = Inventory::PART;
          screen.info.parts.cost = 122;strcpy(screen.info.parts.supplier,"hw");strcpy(screen.info.parts.unit,"pcs");
          struct Inventory shell;
          strcpy(shell.partno,"a001");
          shell.quan = 4;
          shell.type = Inventory::ASSEMBLY;
          shell.info.assembly.n_parts=22;strcpy(shell.info.assembly.parts[0].partno,"d001");
          shell.info.assembly.parts[1].quan = 5;int costs;if(shell.type == Inventory::ASSEMBLY)
          costs = shell.info.assembly.n_parts;
          printf("%d\n",costs); //22
          getchar();return 0;
          }

          6 結構體是不同數(shù)據(jù)類型的數(shù)據(jù)依次存儲在一起

          結構體各數(shù)據(jù)成員的引用可以通過其內(nèi)存大小和字節(jié)對齊來相對于基址偏移來計算。結構體通常用于描述某一事物,用其成員來描述該事物的某些關鍵屬性。讓該事物既可以用結構體變量整體表示,也可以對其成員分別引用來處理該事物的各個屬性。

          #include <stdio.h>int main()
          { struct demo{char a; short b;int c;} abc; // 成員相對于基址偏移,字節(jié)對齊
          abc.b=12; short *p = (short*)((int)&abc+sizeof(short)); // 模擬編譯器計算第2個成員的偏移地址
          printf("%d %d\n",abc.b,*p); // 12 12
          printf("%d\n",sizeof(struct demo));// 8
          getchar();
          }

          7 位域是對整型數(shù)據(jù)的按位處理

          (一次可以處理n個位,1<=n<=整形長度)

          位域(全局)二進制位顯示:

          #include <stdio.h>void printBinM(unsigned int n){    for(int i=31;i>=0;i--)        printf("%d",(n & 1<<i)>>i);    printf("\n");
          }struct Bf{
          unsigned a:3;
          unsigned b:4;
          unsigned c:5;
          }bf;int main(){
          bf.a =1;
          bf.b=15;
          bf.c=3; int *p = (int*)&bf; // 505
          printf("%d\n",*p);
          printBinM(*p);//00000000000000000000000111111001
          getchar();
          }

          位域(局部)二進制位顯示:

          #include <stdio.h>void printBinM(unsigned int n){    for(int i=31;i>=0;i--)        printf("%d",(n & 1<<i)>>i);    printf("\n");
          }int main(){ struct Bf{
          unsigned a:3;
          unsigned b:4;
          unsigned c:5;
          }bf;
          bf.a =1;
          bf.b=15;
          bf.c=3; int *p = (int*)&bf; // -858996231
          printf("%d\n",*p);
          printBinM(*p);//11001100110011001100000111111001
          getchar();
          }

          版權申明:內(nèi)容來源網(wǎng)絡,版權歸原創(chuàng)者所有。除非無法確認,都會標明作者及出處,如有侵權,煩請告知,我們會立即刪除并致歉!


          瀏覽 58
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  婷婷视频免费毛片在线观看 | 国产乱码一区二区三区的区别 | 亚洲国产一级视频 | 中文字幕国产在线观看 | GOGO大胆无码人体免费视频 |