C 庫函數 - gets()

C 標準庫 - <stdio.h> C 標準庫 - <stdio.h>

描述

C 庫函數 char *gets(char *str) 從標準輸入 stdin 讀取一行,並把它存儲在 str 所指向的字串中。當讀取到換行符時,或者到達檔末尾時,它會停止,具體視情況而定。

聲明

下麵是 gets() 函數的聲明。

char *gets(char *str)

參數

  • str -- 這是指向一個字元數組的指針,該數組存儲了 C 字串。

返回值

如果成功,該函數返回 str。如果發生錯誤或者到達檔末尾時還未讀取任何字元,則返回 NULL。

實例

下麵的實例演示了 gets() 函數的用法。

#include <stdio.h>

int main()
{
   char str[50];

   printf("請輸入一個字串:");
   gets(str);

   printf("您輸入的字串是:%s", str);

   return(0);
}

讓我們編譯並運行上面的程式,這將產生以下結果:

請輸入一個字串:zaixian
您輸入的字串是:zaixian

C 標準庫 - <stdio.h> C 標準庫 - <stdio.h>