需要一个额外的变量。 [英] need for an extra variable.
问题描述
大家好,
考虑以下代码片段(来自K& R,第78页)
while((s [0] = c = getch())==''''|| c ==''\ t'')
;
我的问题是,为什么会我们需要变量''c''。
我们可以很容易地把它写成,(b [0] = getch())= =''''|| s [0] ==''\ t'')
;
chandanlinster schrieb:
大家好,
考虑以下代码片段(来自K& R,pg78)
while((s [0] = c = getch())==''''|| c ==''\ t' ')
;
我的问题是,为什么我们需要变量''c''。
我们可以很容易把它写成,
while((s [0] = getch())==''''|| s [0] ==''\ t'')
;
我不知道整个代码,
但是上面的代码c也得到getch()的值/>
并将立即用c ==''\ t''检查。
chandanlinster说:
< blockquote class =post_quotes>
大家好,
考虑以下代码片段(来自K& R,pg78)
while( (s [0] = c = getch())==''''|| c ==''\ t'')
;
我的问题是,为什么我们需要变量''c''。
我们可以很容易地把它写成,
while((s [ 0] = getch())==''''|| s [0] ==''\ t'')
;
c在该函数中使用了好几次,这包括在这里分配给它的值
。在没有c的情况下重写它可以证明是一个有趣的
练习。我建议你这样做,彻底测试你的变化。然后在结果代码中查看
,并将其与K& R'的原始代码进行比较。如果您认为您的
更换更清晰(如果它的工作方式相同),那么请随意使用
。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上面的域名(但显然放弃了www)
在文章< 11 **** ******************@m73g2000cwd.googlegroups .com> ;,
chandanlinster< ch ************ @ gmail.comwrote:
while((s [0] = c = getch())==''''|| c ==''\ t'')
>我的问题是,为什么我们需要变量''c''。
有什么类型的s [0]和c? getch()返回的类型是什么?
- Richard
hello everybody,
consider the following code snippet (from K & R, pg78)
while ((s[0] = c = getch()) == '' '' || c == ''\t'')
;
my question is, why would we need the variable ''c''.
We could easily write it as,
while ((s[0] = getch()) == '' '' || s[0] == ''\t'')
;
chandanlinster schrieb:
hello everybody,
consider the following code snippet (from K & R, pg78)
while ((s[0] = c = getch()) == '' '' || c == ''\t'')
;
my question is, why would we need the variable ''c''.
We could easily write it as,
while ((s[0] = getch()) == '' '' || s[0] == ''\t'')
;I don''t know the whole code,
but with the code above c gets also the value from getch()
and will be checked immediately with c==''\t''.
chandanlinster said:
hello everybody,
consider the following code snippet (from K & R, pg78)
while ((s[0] = c = getch()) == '' '' || c == ''\t'')
;
my question is, why would we need the variable ''c''.
We could easily write it as,
while ((s[0] = getch()) == '' '' || s[0] == ''\t'')
;c is used several times later in that function, and this includes the value
assigned to it here. Rewriting it without c could prove an interesting
exercise. I suggest you do so, testing your changes thoroughly. Then look
at the resulting code, and compare it to K&R''s original. If you think your
replacement is clearer (and if it works identically), then feel free to use
it instead.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
In article <11**********************@m73g2000cwd.googlegroups .com>,
chandanlinster <ch************@gmail.comwrote:
while ((s[0] = c = getch()) == '' '' || c == ''\t'')
>my question is, why would we need the variable ''c''.What are the types of s[0] and c? What is the type returned by getch()?
-- Richard
这篇关于需要一个额外的变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!