#include <stdio.h>   
#pragma pack( push, 1 )
union type
{
    unsigned char udpdatatype;
    struct {
        unsigned int version:4;
        unsigned int reserved_sign:2;
        unsigned int needack_sign:1;
        unsigned int resend_sign :1;
    } protocol;
};
 
#pragma pack( pop )
 
int main(int argc, const char *argv[])
{
    printf("sizeof: %u.\n", sizeof(union type));
    return 0;
}

试着在gcc和VC中运行下,看看sizeof是多少,修改struct中的unsigned int 为unsigned char再看看:

1)unsigned int 情况下,sizeof在gcc下为1,VC下为4
2)unsigned char情况下,sizeof在gcc下为1,VC下也为1