Hey all,
I am reading K&R second edition and am kind of befuddled by one of their examples (a simple program to find and output the longest line). At the begining of main() i placed a line comment () just previous to the section that i find challenging. It seems that by calling function getline() the value of the character array line[] is defined. That value is then used after the function is called. This doesn't make sense to me. After the function is called the values of its parameters should no longer be there, since we are out of scope????
Here is the example:
Code:
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
main()
{
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max=0;
//the parameter line[], is defined by calling the fcn??? aren't we out of scope?
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;
}