this program counts lines and prints out the longest line from input.
Code:
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if(len > max) {
max = len;
copy(longest, line);
}
if(max > 0)
printf("%s", longest);
return 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;
}
void copy(char to[], char from[])
{
int i;
i = 0;
while((to[i] = from[i]) != '\0')
++i;
}
ok im reading the c programming language 2nd edition ever since i started, everything from the beginning until now i was able to understand.
i dont get alot of things in this program so i will be asking tons of questions about this...
1. the book doesnt explain '\0' very clear about how it works in this program, only to what it does... can someone explain to me the purpose of it here?
2.The "if" statement in getline has the same statement body as the for loop so what was the point of that??
3. How does copy function in main if it doesnt return anything??
4. in the for loop in getline, why is it"i < lim -1" and not i<lim? doesnt lim already represents max?
sorry and can u please give me a simple explanation of the entire program too?... those questions were things i dont understand but a clear explanation of the entire program would help me even further. thanks...