Quick actions

cmd+k|ctrl+k

Navigation

Languages

结构体变量占据的内存大小

Snippet info

Language

C

Visibility

public

Author

qwas

Created

2024-05-11T07:12:18.285576Z

Updated

2024-05-11T07:12:18.285576Z

#include <stdio.h>
/**
 * 结构体变量占据的内存大小
 *
 */

struct sample1
{
    char c;  // char 类型占据 1个字节
    short s; // short 类型 占据 2个字节
    float f; // float 类型 占据 4个字节
};
struct sample2
{
    char c;  // char 类型占据 1个字节
    float f; // float 类型 占据 4个字节
    short s; // short 类型 占据 2个字节
};

int main()

{
    struct sample1 Example1;
    struct sample2 Example2;
    printf("结构体变量 Example1 占据的内存字节数是 %zd\n", sizeof(Example1));
    printf("结构体变量 Example2 占据的内存字节数是 %zd\n", sizeof(Example2));

    return 0;
}
INFO