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版)
134
|
第
8
章
如果在定义数组时没有指定其长度,那么元素指示符的索引值可以是任何的非负整数
值。因此,下面的定义会创建一个有
1001
个元素的数组。
int a[ ] = { [1000] = -1 };
所有的数组元素都具有初始值
0
,但最后一个元素例外,它的初始值是-
1
。
8.4 字符串
字符串是连续的字符序列,最后以空字符
'\0'
作为终止符。一个字符串的长度指所有字
符的数量,但不包括终止符。在
C
语言中,没有字符串类型,自然也就没有运算符以字
符串为操作数。
字符串被存储在元素类型为
char
或宽字符类型数组中(宽字符类型指
wchar_t
、
char16_t
或
char32_t
)。宽字符组成的字符串也称为宽字符串(
wide string
)。
C
标准库提供了大
量的函数,它们可以对字符串进行基本操作,例如字符串的比较、复制和连接等。在这
些传统的字符串函数以外,
C11
新增了这些函数的“安全”版本,它们能确保字符串操
作不会超出数组的边界(请参见
17.4
节)。
可以使用字符串字面量来初始化任何字符类型数组。例如,下面两个数组的定义是等
价的:
char str1[30] = "Let's go"; //
字符串长度:
8
;数组长度:
30
char str1[30] = { 'L', 'e', 't', '\'', 's',' ', 'g', 'o', '\0' };
存储字符串的数组一定比字符串长度多一个元素,以容纳下字符串终止符(空字符
'\0'
)。因此,
str1
数组能够存储的字符串最大长度是
29
。如果定义数组长度为 ...
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