2012年6月20日水曜日

開発環境

『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の9章(変数スコープおよび関数)9.7(プログラミング実習)実習9-1を解いてみる。

実習9-1.

コード(TextWrangler)

#include <stdio.h>
int length(char string[]){
  int index;
  for(index = 0; string[index] != '\0'; ++index);
  return (index - 1);
}

int main(){
  char line[100];
  while(1){
    printf("Enter line:");
    fgets(line,sizeof(line),stdin);
    printf("Length (excluding newline) is: %d\n",
      length(line));
  }
  return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
Enter line:12345
Length (excluding newline) is: 5
Enter line:a      
Length (excluding newline) is: 1
Enter line:
Length (excluding newline) is: 0
Enter line:abcde
Length (excluding newline) is: 5
Enter line:^C
$

0 コメント:

コメントを投稿