Skip to content

Instantly share code, notes, and snippets.

@icsaas
Created April 20, 2014 02:11
Show Gist options
  • Save icsaas/11103123 to your computer and use it in GitHub Desktop.
Save icsaas/11103123 to your computer and use it in GitHub Desktop.
#include <stdio.h>
main()
{
struct A {
int a;
char b;
short c;
};
struct B {
char b;
int a;
short c;
};
#pragma pack(2) /*指定按2字节对齐*/
struct C {
char b;
int a;
short c;
};
#pragma pack() /*取消指定对齐,恢复缺省对齐*/
#pragma pack(1) /*指定按1字节对齐 */
struct D {
char b;
int a;
short c;
};
#pragma pack() /*取消指定对齐,恢复缺省对齐 */
int s1=sizeof(struct A);
int s2=sizeof(struct B);
int s3=sizeof(struct C);
int s4=sizeof(struct D);
printf("%d\n",s1);
printf("%d\n",s2);
printf("%d\n",s3);
printf("%d\n",s4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment