W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
您可以使用單個(gè)printf()語句更改程序以分開的行顯示兩個(gè)句子。
#include <stdio.h>
int main(void)
{
printf("This is a test.\nThis is the second line.\n");
return 0;
}
在第一句之后,在文本末尾,你插入了\n
。
組合 \n
是表示換行符的轉(zhuǎn)義序列。
這將導(dǎo)致輸出光標(biāo)移動(dòng)到下一行。
反斜杠(\)表示轉(zhuǎn)義序列的開始。
反斜杠后的字符表示轉(zhuǎn)義序列所代表的字符。
\n
是換行符。
上面的代碼生成以下結(jié)果。
因?yàn)榉葱备鼙旧硎且粋€(gè)特殊字符,要在文本字符串中指定反斜杠,請(qǐng)使用兩個(gè)反斜杠: \\
。
#include <stdio.h>
int main(void)
{
printf("\"This is a test.\"\nShakespeare\n");
return 0;
}
輸出雙引號(hào)是因?yàn)槟谧址惺褂棉D(zhuǎn)義序列。
Shakespeare出現(xiàn)在下一行,因?yàn)樵?code> \“后面有一個(gè) \n
轉(zhuǎn)義序列。
您可以在輸出字符串中使用 \a
轉(zhuǎn)義序列來發(fā)出嗶聲來表示有趣或重要的信號(hào)。
上面的代碼生成以下結(jié)果。
#include <stdio.h>
int main(void)
{
printf("Be careful!!\n\a");
return 0;
}
\a
序列表示“響鈴"字符。
上面的代碼生成以下結(jié)果。
下表顯示了您可以使用的所有轉(zhuǎn)義序列。
轉(zhuǎn)義序列 | 描述 |
---|---|
\n | 表示換行符 |
\r | 表示回車 |
\b | 表示退格 |
\f | 表示換頁字符 |
\t | 表示水平制表符 |
\v | 表示垂直選項(xiàng)卡 |
\a | 插入響鈴(警報(bào))字符 |
\? | 插入問號(hào)(?) |
\" | 插入雙引號(hào)(“) |
\" | 插入單引號(hào)(') |
\\ | 插入反斜杠(\) |
以下代碼顯示了如何使用表中列出的轉(zhuǎn)義字符。
#include <stdio.h> // Include the header file for input and output
int main(void)
{
printf("Hi there!\n\n\nThis program is a bit\n");
printf(" longer than the others.\n");
printf("\nThis is a test.\n\n\n\a\a");
printf("Hey!! What was that???\n\n");
printf("\t1.\tA control character?\n");
printf("\n\t\tThis is a new line?\n\n");
return 0;
}
上面的代碼生成以下結(jié)果。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: