我正在研究 K&R,我试图编写一个程序来打印所有大于 80 个字符的输入行。一旦我从终端自己运行程序,我什么也得不到。谁能告诉我哪里出错了?还有一个我不确定的部分是这条线s[i] = '\0';
- 有人可以向我解释一下这是做什么的吗?
#include <stdio.h>
#define MAXLINE 1000
#define LENGTH 80
int get_line(char line[], int maxline);
/* program to print all lines longer than 80 characters */
main()
{
int len; /*current line length*/
char line[MAXLINE]; /*current input line*/
while((len = get_line(line, MAXLINE)) > 0) /*while length of the line is greater than zero*/
if (len > LENGTH) { /*if the length is greater than 80*/
printf("%s", line); /*print that line*/
return 0;
}
}
/* getline: read a line into s, return length */
int get_line(char s[], int lim)
{
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) /*if i is < 1000 & input is = EOF & input is != newline add one*/
s[i] = c; /*assign a value to each character*/
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0'; /*unsure what '\0' does*/
return i; /*return the length*/
}