C語言字符串的輸入輸出


字符串的輸出

在C語言中,輸出字符串的函數有兩個:

  • puts():直接輸出字符串,並且只能輸出字符串。
  • printf():通過格式控制符 %s 輸出字符串。除了字符串,printf() 還能輸出其他類型的數據。


這兩個函數前面已經講過了,這里不妨再演示一下,請看下面的代碼:

  1. #include <stdio.h>
  2. int main(){
  3. int i;
  4. char str[] = "http://c.biancheng.net";
  5. printf("%s\n", str); //通過變量輸出
  6. printf("%s\n", "http://c.biancheng.net"); //直接輸出
  7. puts(str); //通過變量輸出
  8. puts("http://c.biancheng.net"); //直接輸出
  9. return 0;
  10. }

運行結果:
http://c.biancheng.net
http://c.biancheng.net
http://c.biancheng.net
http://c.biancheng.net

在 printf() 函數中使用%s輸出字符串時,在變量列表中給出數組名即可,不能寫為printf("%s", str[]);

字符串的輸入

在C語言中,輸入字符串的函數有兩個:

  • scanf():通過格式控制符 %s 輸入字符串。除了字符串,scanf() 還能輸入其他類型的數據。
  • gets():直接輸入字符串,並且只能輸入字符串。

1) 使用 scanf() 讀取字符串

請先看下面的例子:

  1. #include <stdio.h>
  2. int main(){
  3. char str1[30], str2[30];
  4. printf("Input str1: ");
  5. scanf("%s", str1);
  6. printf("Input str2: ");
  7. scanf("%s", str2);
  8. printf("str1: %s\nstr2: %s\n", str1, str2);
  9. return 0;
  10. }

運行結果:
Input str1: c.biancheng.net↙
Input str2: Java Python C-Sharp↙
str1: c.biancheng.net
str2: Java

由於字符數組長度為30,因此輸入的字符串長度必須小於30,以留出一個字節用於存放字符串結束標志`\0`。

對程序的說明:
① 我們本來希望將 "Java Python C-Sharp" 賦值給 str2,但是 scanf() 只讀取到 "Java",這是因為 scanf() 讀取到空格時就認為字符串輸入結束了,不會繼續讀取了。請看下面的例子:

  1. #include <stdio.h>
  2. int main(){
  3. char str1[20], str2[20], str3[20];
  4. printf("Input string: ");
  5. scanf("%s", str1);
  6. scanf("%s", str2);
  7. scanf("%s", str3);
  8. printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
  9. return 0;
  10. }

運行結果:
Input string: Java Python C-Sharp↙
str1: Java
str2: Python
str3: C-Sharp

第一個 scanf() 讀取到 "Java" 后遇到空格,結束讀取,將"Python C-Sharp" 留在緩沖區。第二個 scanf() 直接從緩沖區中讀取,不會等待用戶輸入,讀取到 "Python" 后遇到空格,結束讀取,將 "C-Sharp" 留在緩沖區。第三個 scanf() 讀取緩沖區中剩下的內容。

關於緩沖區的知識,我們在《C語言緩沖區(緩存)詳解》《結合C語言緩沖區談scanf()函數》兩節中已經進行了詳細講解。

② 在《從鍵盤輸入數據》中講到,scanf 的各個變量前面要加取地址符&,用以獲得變量的地址,例如:

int a, b;
scanf("%d %d", &a, &b);

但是在本節的示例中,將字符串讀入字符數組卻沒有使用&,例如:

char str1[20], str2[20], str3[20], str4[20];
scanf("%s %s %s %s",str1, str2, str3, str4);

這是因為C語言規定,數組名就代表了該數組的地址。整個數組是一塊連續的內存單元,如有字符數組char c[10],在內存可表示為:

C語言還規定,數組名所代表的地址為第0個元素的地址,例如char c[10];c就代表c[0]的地址。第0個元素的地址就是數組的起始地址,稱為首地址。也就是說,數組名表示數組的首地址。

設數組c的首地址為0X2000,也即c[0]地址為0X2000,則數組名c就代表這個地址。因為c已經表示地址,所以在c前面不能再加取地址符&,例如寫作scanf("%s",&c);是沒有道理的。

有了首地址,有了字符串結束符'\0',就可以在內存中完整定位一個字符串了。例如:

printf("%s", c);

printf 函數會根據數組名找到c的首地址,然后逐個輸出數組中各個字符直到遇到 '\0' 為止。

int、float、char 類型的變量表示數據本身,數據就保存在變量中;而數組名表示的是數組的首地址,數組保存在其他內存單元,數組名保存的是這塊內存的首地址。后面我們會講解指針,大家將會有更加深刻的理解。

2) 使用 gets() 讀取字符串

gets 是 get string 的縮寫,意思是獲取用戶從鍵盤輸入的字符串,語法格式為:

gets(arrayName);

arrayName 為字符數組。從鍵盤獲得的字符串,將保存在 arrayName 中。請看下面的例子:

  1. #include <stdio.h>
  2. int main(){
  3. char str1[30], str2[30];
  4. printf("Input str1: ");
  5. gets(str1);
  6. printf("Input str2: ");
  7. gets(str2);
  8. printf("str1: %s\nstr2: %s\n", str1, str2);
  9. return 0;
  10. }

運行結果:
Input str1: Java Python C-Sharp↙
Input str2: http://c.biancheng.net↙
str1: Java Python C-Sharp
str2: http://c.biancheng.net

可以發現,當輸入的字符串中含有空格時,輸出仍為全部字符串,這說明 gets() 函數不會把空格作為輸入結束的標志,而只把回車換行作為輸入結束的標志,這與 scanf() 函數是不同的。

總結:如果希望讀取的字符串中不包含空格,那么使用 scanf() 函數;如果希望獲取整行字符串,那么使用 gets() 函數,它能避免空格的截斷。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM