?? c75.htm
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>應(yīng)用 </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 應(yīng)用</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">字符轉(zhuǎn)換和算術(shù)運(yùn)算</a></td>
<td width="20%" align="center"><a href="c75.htm#c754.html#c754">字符串,結(jié)構(gòu)和數(shù)組</a></td>
<td width="20%" align="center"><a href="c75.htm#c755.html#c755">命令行參數(shù)</a></td>
</tr>
</table>
<hr>
<h3><a name="c751"></a>1.換碼字符</h3>
<blockquote>
<p>我們已經(jīng)間接提到過<font color="#FF0000">反斜杠符</font>有一種特殊的意思,
就是在形成<font color="#FF0000">新行</font>和<font color="#FF0000">空字符</font>時超出了它本身的用法。正如反斜杠和字符n
的組合 \n 時可以使后面的內(nèi)容從新的一行開始打印,
其它的字符與反斜杠組合時也可以完成特殊的功能。這些反斜杠字符通常被稱為<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">續(xù)行</td>
</tr>
<tr>
<td width="50%" align="center">\nnn</td>
<td width="50%" align="center">字符值</td>
</tr>
</table>
</center></div><p>大多數(shù) CRT 終端都可顯示出表中前六個字符,
完成相應(yīng)的功能。<br>
<br>
例如, 若一字符串中包括退格符 \b, 只要終端有能力正確執(zhí)行此功能,那么當(dāng)終端在顯示該字符時就從
'\b' 所在串的位置處退回一格。<br>
例子<br>
printf("Hello, every\b body!");<br>
輸出 Hello, everbody!<br>
<br>
類似的, 下面的函數(shù)調(diào)用將顯示 a 的值(100),
然后跳過若干空格在下一表區(qū)顯示 b 的值 (3),
再跳過若干空格在下一表區(qū)顯示 c 的值 (27)。<font color="#FF0000">水平制表符</font>對于把各列上的數(shù)據(jù)放在一行上是很有用的。<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
語言編譯器忽略行的結(jié)束,
最基本的是用于把一長字符串續(xù)行到下一行。</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%">沒有續(xù)行符的話, 大多數(shù) C
語言編譯器將對那些企圖跨過多行的初始化字符串會產(chǎn)生出錯信息。</td>
<td width="50%">在每一個要續(xù)行的行后面加一個反斜杠符 \
可以跨行輸入一字符串常量。</td>
</tr>
</table>
<p class="note">注意: 字符串常量必須在下一行的<font color="#FF0000">開頭</font>開始連續(xù)輸入,這是十分必要的,
因?yàn)椴贿@樣的話行頭上的各空白符將被存入字符串。</p>
<p>在反斜杠符表中的最后一項(xiàng),實(shí)際上允許了在一個字符串中包括任意字符,在換碼符
\nnn 中, nnn是代表字符值的一至三個<font color="#FF0000">八進(jìn)制</font>數(shù),
這樣就使得在鍵盤上不能直接輸入的字符可以加入一個字符串中。<br>
<br>
例如, 在 ASCII 碼字符中, "響鈴"字符值為 '\7', 在大多數(shù)終端上,
顯示這個字符將引起一聲鈴響或一陣短的蜂鳴, 為了將<font
color="#FF0000">響鈴符</font>放入一個字符串中, 換碼符 '\7'(或 '\007')可以如下寫入這個串。<br>
printf("\7\7\7SHUT DOWN!\n");<br>
<br>
<font color="#FF0000">空字符</font> '\0' 是上面情況中的一種特殊情形,
它代表值為 0 的字符。實(shí)際上, 由于空字符的值為 0,
這個知識在涉及變長字符串的長度的測試和循環(huán)中是經(jīng)常使用到的。<br>
<br>
例如, 在第四課中, 計算字符串長度的循環(huán)是這樣的: <br>
while (string[count]!='\0') count++; <br>
可以寫成以下等效的形式: while(string[count]) count++;<br>
<br>
在結(jié)束關(guān)于換碼字符的討論前,
應(yīng)當(dāng)再一次強(qiáng)調(diào)的是這些字符在串中只是被當(dāng)作<font color="#FF0000">單個字符</font>。所以字符串
"\007\"Hello\"\n" 實(shí)際上包括 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>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -