没有合适的资源?快使用搜索试试~ 我知道了~
printf is the C language function to do formatted printing. The same function is also available in PERL. This paper explains how printf works, andhow to design the proper formatting specification for any occasion. Background In the early days, computer programmers would write their own subroutines to read in and print out numbers. It is not terribly difficult, actually. Just allocate a character array to hold the result, divide the number by ten, keep the remainder, add x30 to it, and store it.
资源推荐
资源详情
资源评论
Secrets of “printf”
Professor Don Colton
Brigham Young University Hawaii
printf is the C language function to do format-
ted printing. The same function is also available in
PERL. This paper explains how printf works, and
how to design the prop er formatting specification for
any occasion.
1 Background
In the early days, computer programmers would
write their own subroutines to read in and print out
numbers. It is not terribly difficult, actually. Just
allocate a character array to hold the result, divide
the number by ten, keep the remainder, add x30 to
it, and store it at the end of the array. Repeat the
process until all the digits are found. Then print it.
Too easy, right?
But even though it was e asy (for Einstein), it still
took some effort. And what about error checking,
and negative numbers? So the computer program-
mers brought forth libraries of prerecorded func-
tions. And it was good. Eventually the most popular
of these functions were canonized into membership
in the “standard” libraries. Number printing was
popular enough to gain this hallowed honor.
This meant that programmers did not have to
reinvent the number-printing subroutine again and
again. It also meant that everybody’s favorite op-
tions tried to make it into the standard.
Thus was printf born.
2 Simple Printing
In the most simple case, printf takes one argument:
a string of characters to be printed. This string is
composed of characters, each of which is printed ex-
actly as it appears. So printf("xyz"); would sim-
ply print an x, then a y, and finally a z. This is not
exactly “formatted” printing, but it is still the basis
of what printf does.
2.1 Naturally Special Characters
To identify the start of the string, we put a double-
quote (") at the front. To identify the end of the
string we put another double-quote at the end. But
what if we want to actually print a double-quote?
We can’t exactly put a double-quote in the middle
of the string because it would be mistaken for the
end-of-string marker. Double-quote is a special char-
acter. The normal print-what-you-see rules do not
apply.
Different languages take different approaches to
this problem. Some require the special character to
be entered twice. C uses backslash (virgule, \) as
an escape character to change the meaning of the
next character after it. Thus, to print a double-
quote you type in backslash double-quote. To print
a backslash, you must escape it by typing another
backslash in front of it. The first backslash means
“give the next character its alternate meaning.” The
second backslash has an alternate meaning of “print
a backslash.”
Without a backslash, special characters have a
natural special meaning. With a backslash they
print as they appear. Here is a partial list.
\ escape the next character
\ print a backslash
" start or end of string
\" print a double quote
’ start or end a character constant
\’ print a single quote
% start a format specification
\% print a percent sign
2.2 Alternately Special Characters
On the other hand we have characters that normally
print as you would expect, but when you add a back-
slash, then they become special. An example is the
newline character. To print an n, we simply type in
an n. To print a newline, we type in a , thus in-
资源评论
bestkain233
- 粉丝: 24
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功