Hello,
I tried to experiment with the code in K & R chapter 1.9 (a program that reads a set of text lines and prints the longest) and I was getting weird results when entering EOF (pressing CTRL+D on my system).
The code of the function in question is posted below. I set a breakpoint on the line labelled HERE. When I pressed CTRL+D, the breakpoint was never triggered, which means that the for loop didn't end.
Any explanations why, please?
Code: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'; /* HERE */ return i; }



1Likes
LinkBack URL
About LinkBacks



