Skip to Content
C语言核心技术(原书第2版)
book

C语言核心技术(原书第2版)

by Peter Prinz, Tony Crawford
August 2017
Intermediate to advanced
798 pages
17h 44m
Chinese
China Machine Press
Content preview from C语言核心技术(原书第2版)
类型转换
|
59
类型转换
如果
unsignedshort
是
16
位长,那么其最大值
USHRT_MAX
就是十六进制的
FFFF
。当
值
FEDCBA
被转换成
unsigned short
时,其结果与除以十六进制
10000
(即
USHRT_
MAX
+1
),再取余数一样,结果一定是
FFFF
或更小。这个例子中,被赋值给
n
是十六进制
的
DCBA
。
将一个实数浮点数转换为一个无符号或带符号的整数类型,编译器会丢弃小数的部分。
如果剩下的整数部分还超出了新类型的范围,该转换结果为未定义。例如:
double x = 2.9;
unsigned long n = x; // x
的小数部分被直接丢弃
unsigned long m = round (x); //
如果
x
是一个非负数,
//
那么最终语句的效果等同于
m = x + 0.5
在这个例子,初始化
n
的语句中,通过丢弃小数部分
0.9
,
x
的值从
double
转换为
unsigned
long
。整数部分
2
,被赋值给
n
。在初始化
m
的语句中,
C99
函数
round()
会取
x
的近似值,
得到与其最接近的整数(结果可能大于,也可能小于原始值),并返回一个
double
类型
的结果。由于赋值给
m
,这个
double
类型的结果(这个例子中是
3.0
)的小数部分
0
在类型
转换中也会被丢弃。
当一个复数被转换成无符号整数类型时,首先丢弃虚数部分。然后,余下的浮点值会按
照前述那样被转换。范例:
#include <limits.h> //
定义宏,如
UINT_MAX
#include ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)

Publisher Resources

ISBN: 9787111562207