自定义类型题型详解 C语言

 

第一题:求sizeof(union Un)的大小

#include <stdio.h>             
union Un
{
    short s[7];
    int n;
}; 
int main()                                                       
{                                                                         A.14
  printf("%d\n", sizeof(union Un));                     B.4
  return 0;                                                          C.16
}                                                                        D.18

 第二题:默认对齐数为8字节,这个结构体所占的空间大小是多少字节

typedef struct{
  int a;
  char b;           A.16
  short c;          B.9
  short d;          C.12
}AA_t;              D.8

 第三题:下面代码的结果是

#pragma pack(4)/*编译选项,表示4字节对齐 平台:VS2013。语言:C语言*/
//假设long 是4个字节
int main(int argc, char* argv[])
{
  struct tagTest1
  {
    short a;
    char d; 
    long b;   
    long c;   
  };
  struct tagTest2
  {
    long b;   
    short c;
    char d;
    long a;   
  };
  struct tagTest3
  {
    short c;
    long b;
    char d;   
    long a;   
  };
  struct tagTest1 stT1;
  struct tagTest2 stT2;
  struct tagTest3 stT3;

  printf("%d %d %d", sizeof(stT1), sizeof(stT2), sizeof(stT3));
  return 0;
}
#pragma pack()

A.12 12 16   B.11 11 11   C.12 11 16     D.11 11 16

第四题:当A=2, B=3时,pointer分配多少个字节的空间。

#define MAX_SIZE A+B
struct _Record_Struct
{
  unsigned char Env_Alarm_ID : 4;
  unsigned char Para1 : 2;
  unsigned char state;
  unsigned char avail : 1;
}*Env_Alarm_Record;
struct _Record_Struct *pointer = (struct _Record_Struct*)malloc(sizeof(struct _Record_Struct) * MAX_SIZE); 

A.20  B.15  C.11  D.9

 本题字节内存储方向为从高到低!

第五题:在X86下,小端字节序存储,有下列程序;输出结果是多少

#include<stdio.h>
int main()
{
  union
  {
    short k;
    char i[2];
  }*s, a;
  s = &a;
  s->i[0] = 0x39;
  s->i[1] = 0x38;
  printf("%x\n", a.k);
  return 0;
}

 A.3839  B.3938  C.380039  D.不确定

 

第六题:

下面代码的结果是:( )

enum ENUM_A
{
  X1,
  Y1,
  Z1 = 255,
  A1,
  B1,
};
enum ENUM_A enumA = Y1;
enum ENUM_A enumB = B1;
printf("%d %d\n", enumA, enumB);

 A.1, 4  B.1, 257  C.2, 257  D.2, 5

所以enumA=1,enumB=257 

第七题:下面代码的结果是

int main()
{
  unsigned char puc[4];
  struct tagPIM
  {
    unsigned char ucPim1;
    unsigned char ucData0 : 1;
    unsigned char ucData1 : 2;
    unsigned char ucData2 : 3;
  }*pstPimData;
  pstPimData = (struct tagPIM*)puc;
  memset(puc,0,4);
  pstPimData->ucPim1 = 2; 
  pstPimData->ucData0 = 3;
  pstPimData->ucData1 = 4;
  pstPimData->ucData2 = 5;
  printf("%02x %02x %02x %02x\n",puc[0], puc[1], puc[2], puc[3]);
  return 0;
}

A.02 03 04 05  B.02 29 00 00  C.02 25 00 00  D.02 29 04 00

#include<string.h>
#include<stdio.h>
int main()
{
    unsigned char puc[4];
    struct tagPIM
    {
        unsigned char ucPim1;//       一个字节
        unsigned char ucData0 : 1;//  1个比特
        unsigned char ucData1 : 2;//  2个比特
        unsigned char ucData2 : 3;//  3个比特   总和两字节
    }*pstPimData;
    pstPimData = (struct tagPIM*)puc;
    memset(puc, 0, 4);//将puc数组进行赋值 4个0
    pstPimData->ucPim1 = 2;  //将整形2截取并将0000 0010放入
    pstPimData->ucData0 = 3; //将整形3截取并将1放入
    pstPimData->ucData1 = 4; //将整形4截取并将00放入
    pstPimData->ucData2 = 5; //将整形5截取并将101放入
    //二进制最终形式为 00 00 00 10 00 10 10 01 00 00 00 00 00 00 00 00
    printf("%02x %02x %02x %02x\n", puc[0], puc[1], puc[2], puc[3]); //02 29 00 00
    return 0;
}

 第八题:在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是

struct A
{
    int a;
    short b;
    int c;
    char d;
};

struct B
{
    int a;
    short b;
    char c;
    int d;
};

A.16,16  B.13,12  C.16,12  D.11,16

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一串平凡的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值