scanf的格式控制是float,但定义的是int,输出这什么是这个数字呢?
scanf的格式控制和定义的一样,但是输出不一样,为什么输出的都是0呢?
输出和定义的是float,为什么输入是int,于是输出结果就变0了呢?
定义的是float,输入输出是整数型,为什么就变0了呢?
定义的是整数型,输入输出都是浮点型,为什么就不能正常输出呢?
仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
float f;
double d;
char bs[65];
char b[65];
char s[80];
unsigned char *p;
char e[12];
char *t;
int ex;
int flag;
flag=0;
while (1) {
printf("Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:");fflush(stdout);
rewind(stdin);
fgets(s,80,stdin);
if ('\n'==s[0]) return 1;
if (1==sscanf(s,"0x%16I64X",(__int64 *)&d) && strlen(s)>11) {flag=2;break;}
if (1==sscanf(s,"0x%8X" ,( int *)&f)) {flag=1;break;}
if (1==sscanf(s,"%f" , &f)
&& 1==sscanf(s,"%lf" , &d)) {flag=3;break;}
}
if (flag&1) {
printf("f=%g\n",f);
p=(unsigned char *)&f;
printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);
ltoa(*(long *)&f,b,2);
sprintf(bs,"%032s",b);
printf("bin=%s\n",bs);
printf("bin=%.1s %.8s %s\n",bs,bs+1,bs+9);
strncpy(e,bs+1,8);e[8]=0;
ex=strtol(e,&t,2);
printf(" %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);
ex-=127;
printf(" %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);
}
if (flag&2) {
printf("\nd=%lg\n",d);
p=(unsigned char *)&d;
printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]);
_i64toa(*(__int64 *)&d,b,2);
sprintf(bs,"%064s",b);
printf("bin=%s\n",bs);
printf("bin=%.1s %.11s %s\n",bs,bs+1,bs+12);
strncpy(e,bs+1,11);e[11]=0;
ex=strtol(e,&t,2);
printf(" %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);
ex-=1023;
printf(" %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);
}
return 0;
}
//Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:0x3FC0000000000000
//
//d=0.125
//hex=3F C0 00 00 00 00 00 00
//bin=0011111111000000000000000000000000000000000000000000000000000000
//bin=0 01111111100 0000000000000000000000000000000000000000000000000000
// + 1020 -1023 1.0000000000000000000000000000000000000000000000000000
// + -3 1.0000000000000000000000000000000000000000000000000000
//
//Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:0x3E000000
//
//f=0.125
//hex=3E 00 00 00
//bin=00111110000000000000000000000000
//bin=0 01111100 00000000000000000000000
// + 124 -127 1.00000000000000000000000
// + -3 1.00000000000000000000000
//
//Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:0.125
//f=0.125
//hex=3E 00 00 00
//bin=00111110000000000000000000000000
//bin=0 01111100 00000000000000000000000
// + 124 -127 1.00000000000000000000000
// + -3 1.00000000000000000000000
//
//d=0.125
//hex=3F C0 00 00 00 00 00 00
//bin=0011111111000000000000000000000000000000000000000000000000000000
//bin=0 01111111100 0000000000000000000000000000000000000000000000000000
// + 1020 -1023 1.0000000000000000000000000000000000000000000000000000
// + -3 1.0000000000000000000000000000000000000000000000000000
//
//Input a float point number or 0xXXXXXXXX or 0xXXXXXXXXXXXXXXXX:
//