位字段

在存储空间很宝贵的情况下,有可能需要将多个对象保存在一个机器字中。

#include <stdio.h>

struct config
{
    unsigned int c1: 8;
    unsigned int c2: 16;
    unsigned int c3: 8;
};

int main(int argc, char const *argv[])
{
    struct config cfg;
    printf("size: %d.\n", sizeof(cfg));
    cfg.c1 = 255;
    cfg.c2 = 65535;
    cfg.c3 = 255;
    printf("c1: %d, c2: %d, c3: %d.\n", cfg.c1, cfg.c2, cfg.c3);
    return 0;
}

results matching ""

    No results matching ""