c
1コード 2#include <stdio.h> 3#include <stdlib.h> 4#include <sys/types.h> 5#include <unistd.h> 6#include <sys/wait.h> 7#include <string.h> 8#define BUFLEN 1024 /* コマンド用のバッファの大きさ */ 9 10int main(){ 11 char buf[512]; 12 printf("please serch command "); 13 if(fgets(buf, BUFLEN, stdin) == NULL) { 14 } 15 printf("buf is %s", buf); 16 int size = strlen(buf); 17 printf("size = %d\n",size); 18} 19
配列bufに標準入力で文字を入力させてその長さをstrlenで測って出力するプログラムを作ったのですが表示されるサイズが入力した文字列+1の値になります。これはなぜなのでしょうか?配列の終点にはNULLが入っていて文字数ちょうどに値が測れると思ったのですが、、、
実行結果を下に載せます。
please serch command cdc
buf is cdc
size = 4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/10 01:05