📄 c75.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>应用 </title>
<script language="javascript">
var prePage="http://www.nec.sjtu.edu.cn/support/Course/C/c/c7/c/c7/c74.htm";
var nextPage="c/c7/c76.htm";
function showwin(url,winname,properties){
window.open(url,winname,properties)
}
</script>
<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c75.au" loop="1">
</head>
<body background="../img/mainback.jpg" bgproperties="fixed">
<h2 align="center"><font face="楷体_GB2312"><a name="_top"></a>7.5 应用</font></h2>
<table border="0" width="100%">
<tr>
<td width="20%" align="center"><a href="c75.htm#c751.html#c751">换码字符</a> </td>
<td width="20%" align="center"><a href="c75.htm#c752.html#c752">空串</a></td>
<td width="20%" align="center"><a href="c75.htm#c753.html#c753">字符转换和算术运算</a></td>
<td width="20%" align="center"><a href="c75.htm#c754.html#c754">字符串,结构和数组</a></td>
<td width="20%" align="center"><a href="c75.htm#c755.html#c755">命令行参数</a></td>
</tr>
</table>
<hr>
<h3><a name="c751"></a>1.换码字符</h3>
<blockquote>
<p>我们已经间接提到过<font color="#FF0000">反斜杠符</font>有一种特殊的意思,
就是在形成<font color="#FF0000">新行</font>和<font color="#FF0000">空字符</font>时超出了它本身的用法。正如反斜杠和字符n
的组合 \n 时可以使后面的内容从新的一行开始打印,
其它的字符与反斜杠组合时也可以完成特殊的功能。这些反斜杠字符通常被称为<font
color="#FF0000">换码字符</font>, 下表概括了反斜杠的各种用法。</p>
<div align="center"><center><table border="5" width="74%" bgcolor="#CCFFFF"
bordercolor="#FF9933" cellspacing="0" cellpadding="0">
<tr>
<th width="100%" colspan="2" align="center" bgcolor="#FF9933">换码字符</th>
</tr>
<tr>
<td width="50%" align="center">反斜杠字符</td>
<td width="50%" align="center">名字</td>
</tr>
<tr>
<td width="50%" align="center">\b</td>
<td width="50%" align="center">退格</td>
</tr>
<tr>
<td width="50%" align="center">\f</td>
<td width="50%" align="center">走纸</td>
</tr>
<tr>
<td width="50%" align="center">\n</td>
<td width="50%" align="center">新行</td>
</tr>
<tr>
<td width="50%" align="center">\r</td>
<td width="50%" align="center">回车</td>
</tr>
<tr>
<td width="50%" align="center">\t</td>
<td width="50%" align="center">水平制表</td>
</tr>
<tr>
<td width="50%" align="center">\v</td>
<td width="50%" align="center">垂直制表</td>
</tr>
<tr>
<td width="50%" align="center">\\</td>
<td width="50%" align="center">反斜杠</td>
</tr>
<tr>
<td width="50%" align="center">\"</td>
<td width="50%" align="center">双引号</td>
</tr>
<tr>
<td width="50%" align="center">\'</td>
<td width="50%" align="center">单引号</td>
</tr>
<tr>
<td width="50%" align="center">\(CR)</td>
<td width="50%" align="center">续行</td>
</tr>
<tr>
<td width="50%" align="center">\nnn</td>
<td width="50%" align="center">字符值</td>
</tr>
</table>
</center></div><p>大多数 CRT 终端都可显示出表中前六个字符,
完成相应的功能。<br>
<br>
例如, 若一字符串中包括退格符 \b, 只要终端有能力正确执行此功能,那么当终端在显示该字符时就从
'\b' 所在串的位置处退回一格。<br>
例子<br>
printf("Hello, every\b body!");<br>
输出 Hello, everbody!<br>
<br>
类似的, 下面的函数调用将显示 a 的值(100),
然后跳过若干空格在下一表区显示 b 的值 (3),
再跳过若干空格在下一表区显示 c 的值 (27)。<font color="#FF0000">水平制表符</font>对于把各列上的数据放在一行上是很有用的。<br>
<br>
printf("%d\t%d\t%d\n", a, b, c);</p>
<p class="note">为了在一个字符串中包含有反斜杠符本身,
必需使用两个反斜杠符。<br>
printf("\\t is the tab.\n"); 输出 \t is the tab."<br>
注意 由于 \\ 是在串中首先遇到的,所以不显示水平制表符。</p>
<p>为了在字符串中包括一<font color="#FF0000">双引号</font>,必须在前面加上一个反斜杠。<br>
printf("\"Hello!\" he said.\n); 输出 "hello!"
he said.<br>
<br>
为了将一个<font color="#FF0000">单引号</font>字符赋值给 一个字符变量,
单引号前必须有反斜杠符, 如果 c 说明成一个字符类型的变量,
那么语句: c= '\''; 将把一单引号字符赋给 c。<br>
<br>
反斜杠符的后面紧跟一个<font color="#FF0000">回车符</font>,是用来告诉 C
语言编译器忽略行的结束,
最基本的是用于把一长字符串续行到下一行。</p>
<table border="5" width="88%" bgcolor="#CCFFFF" bordercolor="#FF9933" cellspacing="0"
cellpadding="0">
<tr>
<td width="50%" align="center"><font color="#FF0000">错误</font></td>
<td width="50%" align="center"><font color="#FF0000">正确</font></td>
</tr>
<tr>
<td width="50%">static char letter[] = {"abcdefghijklmnopqrst<br>
uvwxyz1234567890"};</td>
<td width="50%">static char letter[] = {"abcdefghijklmnopqrst\<br>
uvwxyz1234567890"};</td>
</tr>
<tr>
<td width="50%">没有续行符的话, 大多数 C
语言编译器将对那些企图跨过多行的初始化字符串会产生出错信息。</td>
<td width="50%">在每一个要续行的行后面加一个反斜杠符 \
可以跨行输入一字符串常量。</td>
</tr>
</table>
<p class="note">注意: 字符串常量必须在下一行的<font color="#FF0000">开头</font>开始连续输入,这是十分必要的,
因为不这样的话行头上的各空白符将被存入字符串。</p>
<p>在反斜杠符表中的最后一项,实际上允许了在一个字符串中包括任意字符,在换码符
\nnn 中, nnn是代表字符值的一至三个<font color="#FF0000">八进制</font>数,
这样就使得在键盘上不能直接输入的字符可以加入一个字符串中。<br>
<br>
例如, 在 ASCII 码字符中, "响铃"字符值为 '\7', 在大多数终端上,
显示这个字符将引起一声铃响或一阵短的蜂鸣, 为了将<font
color="#FF0000">响铃符</font>放入一个字符串中, 换码符 '\7'(或 '\007')可以如下写入这个串。<br>
printf("\7\7\7SHUT DOWN!\n");<br>
<br>
<font color="#FF0000">空字符</font> '\0' 是上面情况中的一种特殊情形,
它代表值为 0 的字符。实际上, 由于空字符的值为 0,
这个知识在涉及变长字符串的长度的测试和循环中是经常使用到的。<br>
<br>
例如, 在第四课中, 计算字符串长度的循环是这样的: <br>
while (string[count]!='\0') count++; <br>
可以写成以下等效的形式: while(string[count]) count++;<br>
<br>
在结束关于换码字符的讨论前,
应当再一次强调的是这些字符在串中只是被当作<font color="#FF0000">单个字符</font>。所以字符串
"\007\"Hello\"\n" 实际上包括 9 个字符: 响铃字符 '\007',
双引号符'\"',Hello 中的五个字符, 又一个双引号符,
以及一个新行符。</p>
<p align="right"><a href="c75.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c752"></a>2.空串</h3>
<blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -