字节对齐 空结构体长度

请仔细查看和对比,尤其是注释掉的结果。

1.代码:

/* 编辑编译环境:Dev-C++ 4.9.9.2 */
/* source: */
#include "stdio.h"
struct S{  };
struct S1{
    char a;
    long b;
    };
struct S2 {
    char c;
    struct S1 d;
    long long e;
    };
struct S3{
    long b;
    char a;
    };
int main(void)
{
                                                            // result:
    printf("sizeof(char) = %d /n",sizeof(char));            // 1
    printf("sizeof(long) = %d /n",sizeof(long));            // 4
    printf("sizeof(long long) = %d /n",sizeof(long long)); // 8
    printf("struct S = %d /n",sizeof(struct S));             // 1
    printf("struct S1 = %d /n",sizeof(struct S1));         // 8
    printf("struct S2 = %d /n",sizeof(struct S2));         // 24
    printf("struct S3 = %d /n",sizeof(struct S3));         // 8

    while(1);
}

2. 提示:

  • 字节对齐
  • 空结构体长度为 1

你可能感兴趣的:(sizeof,字节对齐,空结构体长度,struct结构体长)