K&R C while loop omitting braces
From my understanding you need opening and closing braces for any 'standard construct in the language' (eg. a for loop/if statement etc. I don't know the real word for it) that contains multiple statements. So, why is this K&R C valid...
while((len = getline(line, MAXLINE)) > 0)
if(len > max) {
max = len;
copy(longest, line);
}
There's no braces on the while loop however, it does contain multiple statements (when the if is true). This is from example 1.9 in the 2nd edition of K&R's The C Programming Language.
In your case, there's only one statement under your while loop which is the if
condition. In that case, this code is correct.
Because the if
is read as a single statement body for the while
. It's perfectly valid.
上一篇: 在c ++中分配堆栈数据的生命周期
下一篇: K&R C while循环省略了大括号