标签:原因 getch def 应用程序 signed and ret 代码 慢慢
// intconv.cpp : 定义控制台应用程序的入口点。 // /* intconv.c -- 一些不匹配的整数转换 */ /* 时间:2018年06月18日 00:27:35 代码:程序清单4.11_intconv.c程序_《C Primer Plus》P74 目的:整数转换不匹配的原因,唯有彻底弄清楚补码才明了 */ #include "stdafx.h" #define PAGES 336 #define WORDS 65618 int _tmain(int argc, _TCHAR* argv[]) { short num = PAGES; short mnum = -PAGES; printf("num as short and unsigned short: %hd %hu\n", num, num); printf("-num as short and unsigned short: %hd %hu\n", mnum, mnum); printf("num as int and char: %d %c\n", num, num); printf("WORDS as int, short, and char: %d %hd %c\n", WORDS, WORDS, WORDS); getchar(); return 0; } /* 在VS2010中运行结果: ---------------------------------------------- num as short and unsigned short: 336 336 -num as short and unsigned short: -336 65200 num as int and char: 336 P WORDS as int, short, and char: 65618 82 R ---------------------------------------------- 翻译如下: 数字为 short 和无符号 short:336 336 负数字为 short 和无符号 short:-336 65200 数字为 int和char:336 P 词作为int,short和char:65618 82 R ---------------------------------------------- 总结: 1>.不要期望 %u 转换能把数字和符号分开; 2>.要细心匹配各种不同类型的数值; 3>.唯有在实践中慢慢体会与积累经验; ---------------------------------------------- *程序清单4.11_intconv.c程序_《C Primer Plus》P74
标签:原因 getch def 应用程序 signed and ret 代码 慢慢
原文地址:http://blog.51cto.com/13555061/2130214