The following function is to return the length of a line that is entered through keyboard. But its saying that (The C Programming language K & R) it will return the length of the line, or zero if end of file is encountered.
But when I analyzed with my basic knowledge in C at least it is returning the length of the line till EOF
. So when does it returns 0
. Or my understanding is wrong. Can anybody clarify me ?
以下函数用于返回通过键盘输入的行的长度。但它说(C编程语言K&R)它将返回行的长度,或者如果遇到文件末尾则返回零。但是当我用C中的基本知识进行分析时,它至少会返回行的长度直到EOF。那么它何时返回0.或者我的理解是错误的。任何人都能澄清我吗?
int getline(char s[],int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=’\n’; ++i)
s[i] = c;
if (c == ’\n’) {
s[i] = c;
++i;
}
s[i] = ’\0’;
return i;
}
4 个解决方案
#1
2
When there's nothing, EOF will be there e.g. in case of Empty line, c==EOF
and you have entered a condition in your for
loop that (c=getchar())!=EOF
. Thus i
won't change and when it will be returned after execution of return i;
, it will return 0
什么都没有,EOF会在那里,例如在空行的情况下,c == EOF并且您在for循环中输入了一个条件(c = getchar())!= EOF。因此我不会改变,并且在执行返回i;之后它将返回,它将返回0
I hope this helps.
我希望这有帮助。
#2
2
You analyzed the program correctly.
您正确分析了该程序。
But when I analyzed with my basic knowledge in C at least it is returning the length of the line till EOF
但是当我用C中的基本知识进行分析时,它至少会返回行的长度直到EOF
-> It will return 0 when the line is empty
- >当行为空时它将返回0
#3
0
one of the conditions in the for
loop is (c=getchar())!=EOF
. So when the line is empty, ie. c==EOF
at the first instance itself, it doesnt enter the loop. Hence i
won't get incremented and it returns 0.
for循环中的一个条件是(c = getchar())!= EOF。所以当线条为空时,即。 c == EOF在第一个实例本身,它不进入循环。因此我不会增加并返回0。
#4
0
If the line is empty, it will return 0.
如果该行为空,则返回0。
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=’\n’; ++i)
First, You set i=0;
. If ((c=getchar())==EOF)
, the for loop won't run and i
won't be incremented. The situation is the same when the first character is a \n
(in this case i will be incremented later)
首先,设置i = 0;。如果((c = getchar())== EOF),for循环将不会运行,我将不会递增。当第一个字符是\ n时,情况是相同的(在这种情况下,我将在稍后递增)
#1
2
When there's nothing, EOF will be there e.g. in case of Empty line, c==EOF
and you have entered a condition in your for
loop that (c=getchar())!=EOF
. Thus i
won't change and when it will be returned after execution of return i;
, it will return 0
什么都没有,EOF会在那里,例如在空行的情况下,c == EOF并且您在for循环中输入了一个条件(c = getchar())!= EOF。因此我不会改变,并且在执行返回i;之后它将返回,它将返回0
I hope this helps.
我希望这有帮助。
#2
2
You analyzed the program correctly.
您正确分析了该程序。
But when I analyzed with my basic knowledge in C at least it is returning the length of the line till EOF
但是当我用C中的基本知识进行分析时,它至少会返回行的长度直到EOF
-> It will return 0 when the line is empty
- >当行为空时它将返回0
#3
0
one of the conditions in the for
loop is (c=getchar())!=EOF
. So when the line is empty, ie. c==EOF
at the first instance itself, it doesnt enter the loop. Hence i
won't get incremented and it returns 0.
for循环中的一个条件是(c = getchar())!= EOF。所以当线条为空时,即。 c == EOF在第一个实例本身,它不进入循环。因此我不会增加并返回0。
#4
0
If the line is empty, it will return 0.
如果该行为空,则返回0。
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=’\n’; ++i)
First, You set i=0;
. If ((c=getchar())==EOF)
, the for loop won't run and i
won't be incremented. The situation is the same when the first character is a \n
(in this case i will be incremented later)
首先,设置i = 0;。如果((c = getchar())== EOF),for循环将不会运行,我将不会递增。当第一个字符是\ n时,情况是相同的(在这种情况下,我将在稍后递增)