strm さんが書きました:と、書いていて思ったのですが、インクリメントしていった結果strに格納された「\0」に到達すると、
それは「0」であり偽となってループを抜けるのでしょうか。
つまり\0は0として扱っていいということなのでしょうか。
以下の引用は
N1570よりです。
scanfはfscanfの第一引数にstdinを渡したものと同じです。
7.21.6.4 The scanf function さんが書きました:2 The scanf function is equivalent to fscanf with the argument stdin interposed
before the arguments to scanf.
fscanfの%sは読み込んだデータの後にnull文字を格納します。
7.21.6.2 The fscanf function さんが書きました:s Matches a sequence of non-white-space characters. 286)
If no l length modifier is present, the corresponding argument shall be a
pointer to the initial element of a character array large enough to accept the
sequence and a terminating null character, which will be added automatically.
null文字のビットは全てゼロです。
5.2.1 Character sets さんが書きました: A byte with
all bits set to 0, called the null character, shall exist in the basic execution character set; it
is used to terminate a character string.
よって、scanfで読み込んだ文字列を最初から1文字ずつ見ていくと、いつかnull文字に出会い、そのデータは0です。
while文は、制御式が0になるまでループするので、裏を返せば制御式が0になったら、すなわちnull文字に出会ったらループを抜けます。
6.8.5 Iteration statements さんが書きました:1 iteration-statement:
while ( expression ) statement
(略)
4 An iteration statement causes a statement called the loop body to be executed repeatedly
until the controlling expression compares equal to 0.
ちなみに、'\0'はnull文字を表します。
6.4.4.4 Character constants さんが書きました:12 EXAMPLE 1 The construction '\0' is commonly used to represent the null character.