Linux_WIN7#pragma_##宏_位域

1 篇文章 0 订阅

我家panda问我这题。然后我用32位机子测试是4.

这题目应该不够完美吧。

通过#pragma宏设置紧凑模式。这样的结果可以是3

#include<stdio.h>
#include<string.h>
#pragma pack(1)
struct s
{
 unsigned a:10;
 unsigned b:12;
unsigned c:2;
}x;
#pragma pack()
int main()
{
  printf("%d\n",sizeof(x));
return 0;
}


 

filename gt

compile:gcc gt.c

run :./a.out

 #include<stdio.h>
#include<string.h>
//=================#pragma pack()

struct strubase
{ unsigned char a:2;
unsigned char b:3;
unsigned char c:3;
};

struct  strufur
{ unsigned char a:2;
 unsigned char b:3;
 unsigned char c:3;
 unsigned int d:4;
};
//=================#pragma(pop)
#define  tstrubase(n)  base_pack##n
#define  tstrufur(n)   fur_fur##n
#define  strubase(n)   base_pack##n { unsigned char a:2;\
unsigned char b:3;\
unsigned char c:3;\
}
#define  strufur(n)   fur_fur##n { unsigned char a:2;\
 unsigned char b:3;\
 unsigned char c:3;\
 unsigned int d:4;\
}

//=================#pragma pack(push,1)
#pragma pack(1)
struct strubase(1);
struct  strufur(1);
#pragma pack()
//=================#pragma(pop)
//=================#pragma pack(push,2)
#pragma pack(2)
struct strubase(2);
struct  strufur(2);
#pragma pack()
//=================#pragma(pop)

//=================#pragma pack(push,4)
#pragma pack(4)
struct strubase(4);
struct  strufur(4);
#pragma pack()
//=================#pragma(pop)

//=================#pragma pack(push,8)
#pragma pack(8)
struct strubase(8);
struct  strufur(8);
#pragma pack()
//=================#pragma(pop)


int main()
{
  printf("Linux--------default---------\n");
  struct  strubase x0;
  struct  strufur m0;
  printf("%d\n",sizeof(x0));
  printf("%d\n",sizeof(m0));
  printf("Linux--------default---------\n");


  printf("Linux--------#pragma pcak(1)---------\n");
  struct tstrubase(1) x;
  struct  tstrufur(1) m;
  printf("%d\n",sizeof(x));
  printf("%d\n",sizeof(m));
  printf("Linux--------#pragma pcak()---------\n");

  printf("Linux--------#pragma pcak(2)---------\n");
  struct tstrubase(2) x2;
  struct  tstrufur(2) m2;
  printf("%d\n",sizeof(x2));
  printf("%d\n",sizeof(m2));
  printf("Linux--------#pragma pcak()---------\n");

  printf("Linux--------#pragma pcak(4)---------\n");
  struct tstrubase(4) x4;
  struct  tstrufur(4) m4;
  printf("%d\n",sizeof(x4));
  printf("%d\n",sizeof(m4));
  printf("Linux--------#pragma pcak()---------\n");

  printf("Linux--------#pragma pcak(8)---------\n");
  struct tstrubase(8) x8;
  struct  tstrufur(8) m8;
  printf("%d\n",sizeof(x8));
  printf("%d\n",sizeof(m8));
  printf("Linux--------#pragma pcak()---------\n");
 return 0;
}


 

screen

Linux--------default---------
1
4
Linux--------default---------
Linux--------#pragma pcak(1)---------
1
2
Linux--------#pragma pcak()---------
Linux--------#pragma pcak(2)---------
1
2
Linux--------#pragma pcak()---------
Linux--------#pragma pcak(4)---------
1
4
Linux--------#pragma pcak()---------
Linux--------#pragma pcak(8)---------
1
4
Linux--------#pragma pcak()---------

 

WIN  VC下的.cpp

#include<stdio.h>
#include<string.h>
//=================#pragma pack()

struct strubase
{ unsigned char a:2;
unsigned char b:3;
unsigned char c:3;
};

struct  strufur
{ unsigned char a:2;
 unsigned char b:3;
 unsigned char c:3;
 unsigned int d:4;
};
//=================#pragma(pop)
#define  tstrubase(n)  base_pack##n
#define  tstrufur(n)   fur_fur##n
#define  strubase(n)   base_pack##n { unsigned char a:2;\
unsigned char b:3;\
unsigned char c:3;\
}
#define  strufur(n)   fur_fur##n { unsigned char a:2;\
 unsigned char b:3;\
 unsigned char c:3;\
 unsigned int d:4;\
}

//=================#pragma pack(push,1)
#pragma pack(1)
struct strubase(1);
struct  strufur(1);
#pragma pack()
//=================#pragma(pop)
//=================#pragma pack(push,2)
#pragma pack(2)
struct strubase(2);
struct  strufur(2);
#pragma pack()
//=================#pragma(pop)

//=================#pragma pack(push,4)
#pragma pack(4)
struct strubase(4);
struct  strufur(4);
#pragma pack()
//=================#pragma(pop)

//=================#pragma pack(push,8)
#pragma pack(8)
struct strubase(8);
struct  strufur(8);
#pragma pack()
//=================#pragma(pop)


int main()
{
  printf("VC_.cpp--------default---------\n");
  struct  strubase x0;
  struct  strufur m0;
  printf("%d\n",sizeof(x0));
  printf("%d\n",sizeof(m0));
  printf("VC_.cpp--------default---------\n");


  printf("VC_.cpp--------#pragma pcak(1)---------\n");
  struct tstrubase(1) x;
  struct  tstrufur(1) m;
  printf("%d\n",sizeof(x));
  printf("%d\n",sizeof(m));
  printf("VC_.cpp--------#pragma pcak()---------\n");

  printf("VC_.cpp--------#pragma pcak(2)---------\n");
  struct tstrubase(2) x2;
  struct  tstrufur(2) m2;
  printf("%d\n",sizeof(x2));
  printf("%d\n",sizeof(m2));
  printf("VC_.cpp--------#pragma pcak()---------\n");

  printf("VC_.cpp--------#pragma pcak(4)---------\n");
  struct tstrubase(4) x4;
  struct  tstrufur(4) m4;
  printf("%d\n",sizeof(x4));
  printf("%d\n",sizeof(m4));
  printf("VC_.cpp--------#pragma pcak()---------\n");

  printf("VC_.cpp--------#pragma pcak(8)---------\n");
  struct tstrubase(8) x8;
  struct  tstrufur(8) m8;
  printf("%d\n",sizeof(x8));
  printf("%d\n",sizeof(m8));
  printf("VC_.cpp--------#pragma pcak()---------\n");
 return 0;
}


screen

VC_.cpp--------default---------
1
8
VC_.cpp--------default---------
VC_.cpp--------#pragma pcak(1)---------
1
5
VC_.cpp--------#pragma pcak()---------
VC_.cpp--------#pragma pcak(2)---------
1
6
VC_.cpp--------#pragma pcak()---------
VC_.cpp--------#pragma pcak(4)---------
1
8
VC_.cpp--------#pragma pcak()---------
VC_.cpp--------#pragma pcak(8)---------
1
8
VC_.cpp--------#pragma pcak()---------
Press any key to continue

WIN7 VC下的.c

  #include<stdio.h>
#include<string.h>
//=================#pragma pack()

struct strubase
{ unsigned char a:2;
unsigned char b:3;
unsigned char c:3;
};

struct  strufur
{ unsigned char a:2;
 unsigned char b:3;
 unsigned char c:3;
 unsigned int d:4;
};
//=================#pragma(pop)
#define  tstrubase(n)  base_pack##n x##n
#define  tstrufur(n)   fur_fur##n   m##n
#define  strubase(n)   base_pack##n { unsigned char a:2;\
unsigned char b:3;\
unsigned char c:3;\
}
#define  strufur(n)   fur_fur##n { unsigned char a:2;\
 unsigned char b:3;\
 unsigned char c:3;\
 unsigned int d:4;\
}

//=================#pragma pack(push,1)
#pragma pack(1)
struct strubase(1);
struct  strufur(1);
#pragma pack()
//=================#pragma(pop)
//=================#pragma pack(push,2)
#pragma pack(2)
struct strubase(2);
struct  strufur(2);
#pragma pack()
//=================#pragma(pop)

//=================#pragma pack(push,4)
#pragma pack(4)
struct strubase(4);
struct  strufur(4);
#pragma pack()
//=================#pragma(pop)

//=================#pragma pack(push,8)
#pragma pack(8)
struct strubase(8);
struct  strufur(8);
#pragma pack()
//=================#pragma(pop)


int main()
{
  struct  strubase x0;
  struct  strufur m0;
  struct tstrubase(1);
  struct  tstrufur(1);
  struct tstrubase(2);
  struct  tstrufur(2);
  struct tstrubase(4);
  struct  tstrufur(4);
  struct tstrubase(8);
  struct  tstrufur(8);
   printf("VC_.c--------default---------\n");
  
  printf("%d\n",sizeof(x0));
  printf("%d\n",sizeof(m0));
  printf("VC_.c--------default---------\n");

  printf("VC_.c--------#pragma pcak(1)---------\n");
  printf("%d\n",sizeof(x1));
  printf("%d\n",sizeof(m1));
  printf("VC_.c--------#pragma pcak()---------\n");

  printf("VC_.c--------#pragma pcak(2)---------\n");
 
  printf("%d\n",sizeof(x2));
  printf("%d\n",sizeof(m2));
  printf("VC_.c--------#pragma pcak()---------\n");

  printf("VC_.c--------#pragma pcak(4)---------\n");
 
  printf("%d\n",sizeof(x4));
  printf("%d\n",sizeof(m4));
  printf("VC_.c--------#pragma pcak()---------\n");

  printf("VC_.c--------#pragma pcak(8)---------\n");
  printf("%d\n",sizeof(x8));
  printf("%d\n",sizeof(m8));
  printf("VC_.c--------#pragma pcak()---------\n");
 return 0;
}


screen

VC_.c--------default---------
1
8
VC_.c--------default---------
VC_.c--------#pragma pcak(1)---------
1
5
VC_.c--------#pragma pcak()---------
VC_.c--------#pragma pcak(2)---------
1
6
VC_.c--------#pragma pcak()---------
VC_.c--------#pragma pcak(4)---------
1
8
VC_.c--------#pragma pcak()---------
VC_.c--------#pragma pcak(8)---------
1
8
VC_.c--------#pragma pcak()---------
Press any key to continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值