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版)
53
第4章
类型转换
在
C
语言中,不同类型的运算符可以被混合在一个运算表达式中。例如,下面的表达式
是可行的:
double dVar = 2.5; //
定义
dVar
为
double
类型的变量
dVar *= 3; //
将
dVar
乘以一个整数常量
if ( dVar < 10L ) //
与一个
long
整数常量做比较
{ /*
…
*/ }
当操作数(
operand
)具有不同的类型时,编译器会试着将它们转换成相同的类型,然
后进行计算。在某些例子中,甚至要求必须在程序中插入类型转换命令。一个类型转换
会产生一个新类型的值,可以是
void
类型(它的意思是指,丢弃该表达式的值;请参见
2.6.2
节),或者标量类型,也就是算术类型或指针。例如,一个指向结构的指针类型,
可以被转换为其他指针类型。然而,实际结构类型的值无法被转换为其他结构类型。
当操作数的类型不匹配时,或者当调用一个函数,所使用的实际参数(
argument
)类型
不匹配函数对应的形式参数(
parameter
)类型时,编译器会进行隐式类型转换(
implicit
type conversion
)。当对变量初始化或者赋值时,程序也会进行必要的隐式类型转换。
如果必要的类型转换未能正常执行,编译器就会发出一个错误信息。
也可以使用类型转换运算符(
cast operator
)来显式地将值把一个类型转换为另一个类型
(参见第
5
章):
(
类型名称
)
表达式
在下面的示例中,一个整数变量除以另一个整数变量,在使用类型转换运算符下,该运
算以浮点运算的方式进行: ...
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