简单的C语法问题 [英] Simple C syntax Question

查看:102
本文介绍了简单的C语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑进以下code在C当然,我走的老考试:

 结构QuestionSet {
    CHAR 1:1;
    CHAR Q2:1;
    CHAR Q3:1;
}

我不知道什么样的语法CHAR 1:1的意思,我一直没能找到它的C程序设计语言,这是当然的书的任何地方。任何人都可以解释一下吗?


解决方案

这是一个位域。冒号后的数字表示的比特数分配给该结构元件。所以,这三个元素都是一个位宽,并能存储两个值:0和1或-1(取决于你的编译器,虽然-1会考虑补码运算时,是更合理的选择)<。 / p>

I ran into the following code in an old exam of the C course I'm taking:

struct QuestionSet{
    char q1:1;
    char q2:1;
    char q3:1;
}

I have no idea what the syntax "char q1:1" means, and I haven't been able to find it anywhere in "The C Programming Language" which is the course book. Can anyone explain?

解决方案

It's a bitfield. The number after the colon indicates the number of bits to assign to the struct element. So the three elements are all one bit wide, and are able to store two values: 0, and either 1 or -1 (depending on your compiler, though -1 would be the more logical option when considering two's-complement arithmetic).

这篇关于简单的C语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆