在网上找到一篇展示计算机浮点数格式的文章,且有C代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.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; while (1) { printf("Input a float point number:"); fflush(stdout); rewind(stdin); fgets(s, 80, stdin); if (1 == sscanf(s, "%f", &f) && 1 == sscanf(s, "%lf", &d)) break; } 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); 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; }
遂在自己的64位Windows系统电脑上建立VS工程,粘贴上述代码。编译,出现两个错误。
第一个错误,提示各种函数不安全,要求替换。在网上找到解决方案,并采用更改预处理定义的方法解决了问题,也即:项目->属性->配置属性->C/C++ -> 预处理器 -> 预处理器定义,增加:_CRT_SECURE_NO_DEPRECATE
第二个错误,提示如下:
warning C4996: ‘itoa‘: The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _itoa. See online help for details
在网上找到解决方案,首先采用 PROJECT PROPERTIES->LINKER->GENERAL->ENABLE INCREMENTAL LINKKING and set it to NO 的办法,没有解决了问题。然后,接受编译器的提示和建议,用_itoa函数,编译通过。
时间: 2024-10-12 17:20:02