😸
[C] fgets 関数
機能
fgets関数は、標準入力やファイルから1行分の文字列を取得する関数[1]
使用例
abc
と表示する
\n
は改行を表す エスケープシーケンス[2]
コード
char str[10];
fgets(str, sizeof(str), stdin);
printf("%s", str);
全文
#include <stdio.h>
void main() {
char str[10];
fgets(str, sizeof(str), stdin);
printf("%s", str);
}
実行結果
abc
abc
hello
を変数から表示する
コード
char buf[20];
fgets(buf, 20, stdin);
printf("%s", buf);
全文
#include <stdio.h>
void main() {
char buf[20];
fgets(buf, 20, stdin);
printf("%s", buf);
}
実行結果
hello
hello
Discussion