C语言常见陷阱之“纠缠不清的位域”
淘宝店铺:【吴鉴鹰的小铺】
-
#define _CRT_SECURE_NO_WARNINGS 1
-
#include<stdio.h>
-
#include<stdlib.h>
-
int main()
-
{
-
char c;
-
unsigned char uc;
-
unsigned char us;
-
c = 128;// -128-0-127;
-
uc = 128;
-
us = c + uc;//256;
-
printf("0x%x\n", us);
-
us = (unsigned char)c + uc;
-
printf("0x%x\n", us);
-
us = c + (char)uc;
-
printf("0x%x\n", us);
-
system("pause");
-
return 0;
-
}
-
int main()
-
{
-
char c;
-
unsigned char uc;
-
unsigned char us;
-
c = 128;// -128-0-127;
-
uc = 128;
-
us = c + uc;//256;
-
c是有符号型最高位是符号位1111 1111 1111 1111 1111 1111 1000 0000
-
uc是无符号 0000 0000 0000 0000 0000 0000 1000 0000
-
0000 0000 0000 0000 0000 0000 0000 0000
-
最后的结果截断ox表示16进制取后16位结果是oxoo
-
10000 0000无符号取低八位;为0;
-
printf("0x%x\n", us);
-
-
us = (unsigned char)c + uc;
-
强制后c 0000 0000 0000 0000 0000 0000 1000 0000
-
uc 0000 0000 0000 0000 0000 0000 1000 0000
-
0000 0000 0000 0000 0000 0001 0000 0000
-
结果截断表示ox0f00;
-
printf("0x%x\n", us);
-
-
us = c + (char)uc;
-
c是有符号型最高位是符号位 1111 1111 1111 1111 1111 1111 1000 0000
-
uc是有符号型最高位是符号位 1111 1111 1111 1111 1111 1111 1000 0000
-
1111 1111 1111 1111 1111 1111 0000 0000
-
结果截断表示ox0ff00
-
printf("0x%x\n", us);
-
system("pause");
-
return 0;
-
}
-
int fun(int x, int y)
-
{
-
static int m = 0;
-
static int i = 2;
-
i += m + 1;//3
-
//第二次8+3+1
-
m = i + x + y;//3+x+y
-
//第二次12+4+1
-
return m;//8
-
}
-
int main()
-
{
-
int j = 4;
-
int m = 1;
-
int k;//k = 8
-
k = fun(j, m);// 4 1
-
printf("%d\n", k);
-
k = fun(j, m);//4 1
-
printf("%d\n", k);
-
system("pause");
-
return 0;
-
}
-
struct B
-
{
-
long A1;
-
char cA2;
-
char cA3;
-
long A4;
-
long A5;
-
}*p;
-
p = (struct B*)0x100000;
-
p + 0x1 = 100001;
-
(unsigned long)p + 0x1 = 0x100001;
-
(unsigned long*)p +0x1 = 0x100004;
-
(char *)p + 0x1 = 0x100004;
-
struct B
-
{
-
long A1; //4
-
char cA2; //1
-
char cA3; //1
-
long A4; //4
-
long A5; //4
-
}*p;
-
//4+1+1+2+4+4 = 16
-
p = (struct B*)0x100000;
-
p + 0x1 = 100010;
(unsigned long)p + 0x1 = 0x100001;
(unsigned long*)p +0x1 = 0x100004
(char *)p + 0x1 = 0x100004;
-
#define MAX_SIZE A+B
-
struct _Record _Struct
-
{
-
unsigned char E : 4;
-
unsigned char p : 2;
-
unsigned char stata;
-
unsigned char a : 1;
-
}*p;
-
struct Record _Struct *p = (struct Record _Struct *p)malloc(szieof(struct Record _Struct)*MAX_SZIE);
{ 位域列表 };
-
struct tagAAA
-
{
-
unsigned char a : 1;
-
unsigned char b : 2;
-
unsigned char c : 6;
-
unsigned char d : 4;
-
unsigned char e;
-
unsigned char f : 4;
-
unsigned char g;
-
}AAA_s;
-
int main()
-
{
-
struct tagAAA ;
-
printf("%d\n", sizeof(struct tagAAA));
-
system("pause");
-
return 0;
-
}
-
#pragma pack(4)
-
int main()
-
{
-
struct tagPIM
-
{
-
unsigned char a;
-
unsigned char b : 1;
-
unsigned char c : 2;
-
unsigned char d : 3;
-
}*pstdata;
-
unsigned char puc[4];
-
pstdata = (struct tagPIM*)puc;
-
memset(puc,0, 4);
-
pstdata->a = 2;
-
pstdata->b = 3;
-
pstdata->c = 4;
-
pstdata->d = 5;
-
printf("%02x %02x %02x %02x\n", puc[0], puc[1], puc[2], puc[3]);
-
system("pause");
-
return 0;
-
}
本文章来源网络,如果原作者不支持咱们转发,请联系删除,谢谢!
喜欢本文的亲们,欢迎点赞
技术源于积累,成功来自执着 ——单片机精讲吴鉴鹰 |