I am a beginner and was learning to code using k&r's c programming book. Now there is this example of "a program to input set of text lines and print the longest"
I am giving you the whole 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]='n';
i++;
}
s[i]='';
return i;
}
void copy(char to[], char from[])
{
int i;
i=0;
while((to[i]=from[i])!='')
i++;
}
Now tell me, in getline function, in the statement:
for(i=0;i<lim-1&&(c=getchar())!=EOF&&c!='n';i++)
why do we use " c!='n' "
and what is the meaning of code after this for statement i.e.
s[i]=c;
if(c=='n)
{
s[i]=c;
i++;
}
s[i]=''
why have we used s[i]='' in this?
and i++; statement in if(c=='n') condition?
Aucun commentaire:
Enregistrer un commentaire