时间:2021年4月14日13:38:52
条件编译:#ifdef #else #endif
#ifdef 满足条件编译,不满足则跳过编译。
#include <stdio.h>
#define LENGTH 10
#define JUST_CHECKING
int main(void)
{
int a = 10;
int arr[10] = { 0 };
for (size_t i = 0; i < LENGTH; i++)
{
arr[i] = i;
printf("%d \n", arr[i]);
}
#ifdef JUST_CHECKING
printf("%d \n ", a);
#else
printf("%d \n", a + 1);
#endif
#if 1+1
printf("%d \n", a + 2);
#elif 0
printf("%d \n", a + 3);
#else
printf("%d \n", a + 4);
#endif
return 0;
}
#include<stdio.h>
#include<stddef.h>
#define OFFSETOF(struct_name,member_name) (int *) &((struct_name *)0)->member_name
struct s
{
char a;
int b;
char c;
};
int main(void)
{
printf("%d \n ",offsetof(struct s,a));
printf("%d \n",offsetof(struct s,b));
printf("%d \n",offsetof(struct s,c));
int a=OFFSETOF(struct s, a);
int b=OFFSETOF(struct s, b);
int c=OFFSETOF(struct s, c);
printf("%d \n",a);
printf("%d \n", b);
printf("%d \n", c);
return 0;
}
#include<stdio.h>
struct ppop
{
unsigned char pu1;
unsigned char pum2 : 1;
unsigned char pum3 : 2;
unsigned char pum4 : 3;
}*psppop;
int main()
{
unsigned char puc[4];
psppop = (struct ppop*)puc;
memset(puc, 0, 4);
psppop->pu1 = 2;
psppop->pum2 = 3;
psppop->pum3 = 4;
psppop->pum4 = 5;
printf("%02x %02x %02x %02x \n", puc[0], puc[1], puc[2], puc[3]);
return 0;
}
#include<stdio.h>
union u
{
short a[7];
int b;
};
int main(void)
{
printf("%d \n",sizeof(union u));
return 0;
}