|
5鱼币
插代码,奔主题,不废话:
【问题已在注释中,麻烦大家帮我看下;】
- #include<stdio.h>
- #include<stdlib.h>
- //编辑/编译器:C-Free;4.0;
- //小心误导,终究理解;
- //2013年7月13日02:32:23;
- int main(void){
- int i=-3;
- char str[5];
- typedef struct bin{
- int n:1;
- int on:1;
- int off;//知道为什么可以赋值99吗?因为off是个整型变量,不是位域;
- char nul:1;
- }Bt;
- Bt use;
- use.on=0;
- use.off=99;
- use.nul=1;
- printf("on=%d;off=%d;nul=%d;\n\n",use.on,use.off,use.nul);
-
- while(i<=3){
- use.n=i;
- itoa(use.n,str,2);
- /*[问题1:]2进制输出就出现问题了? 改成use.n或i都一样 ,其他进制没问题*/
-
- printf("use.n=%2d i=%2d bin:%s\n",use.n,i,str);
- i++;
- }
- return 0;
- }
- //[问题2:]C语言如何解决 int n:233;这样的,问题?
复制代码- #include<stdio.h>
- int main(void){
- typedef struct{
- int n:33;
- }B;
- printf("sizeof(B)=%d;\n",sizeof(B));
- return 0;
- }
- /*[问题2延续:] warning: width of `main()::{anonymous struct}::n' exceeds its type;
- * 警告:对` main()宽度::{姓名}:结构:N超过它的类型;
- *这个提示是不是对超过位域长度的有效提示?
- */
复制代码
|
最佳答案
查看完整内容
至于第二个问题
一个int为32位 4字节大小 你强行给他开辟33个位阈自然出错
看一下vs2010编译器的说法
如果这样
#include
int main(void){
typedef struct{
long long n:33;
}B;
printf("sizeof(B)=%d;\n",sizeof(B));
return 0;
}
一个long long默认为8个字节 64位大小
|