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版)
568
|
第
18
章
符。如果
n
足够大,那么函数的返回值是写入缓冲区的字符数量(不计入字符串终止
符)。相应地,当且仅当函数返回一个小于
n
的非负值,才能表示输出字符串被完整写
入。为了获得输出字符串的长度,而不用实际存储输出字符串,可以将
n
设定为
0
;这样
的话,
sprintf()
就不会向
dest
中写入任何字符,这时,
dest
可以是一个空指针。
安全函数
snprintf_s()
等效于
snprintf()
,但它遵循以下运行约束:指针参数
dest
、
format
,以及对应于
%s
转
换修饰符的所有参数,都不得为空指针;
n
必须大于
0
,但是
不得大于
RSIZE_MAX
;格式字符串不得包含转换修饰符
%n
。如果违反了运行约束,
snprintf_s()
向
dest[0]
中
写入一个字符串终止符
'\0'
,这里假设
dest
不是一个空指
针,并且
n
大于
0
小于
RSIZE_MAX
。
如果运行约束全部满足,则
snprintf_s()
的返回值与
snprintf()
类似。如果违反了运行
约束,
snprintf_s()
返
回一个负值。
如果输出缓冲区与
snprintf()
或
snprint_s()
的任一参数所引用的内存区域之间存
在重叠,则函数的行为是未定义的。
示例
char buffer[80];
double x = 1234.5, y = 678.9, z = -753.1, a = x * y + z;
int output_len = 0;
output_len =
snprintf
( buffer , 80, "For the input values ...
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