i'm sorry, i have no idea how to word this. i'm learning c independently out of k&r on a mac. but there's a problem.
so, for example, a very simple program which prints the longest line and length of the line from an input (from k&r)
Code:
#include <stdio.h>
#define MAXLINE 1000 /* max input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
main()
{
int len, max;
char line[MAXLINE], 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;
}
/* getline; read a line into s, return length */
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;
}
/* copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy(char to[], char from[])
{
int i;
while (to[i] = from[i] != '\0')
++ i;
}
when i compile this and run it by ./a.out, i've got the text input (from the getchar()), but the program never advances to the point where it prints the line and length. i used to be able to put in a break trigger in the getchar loop, but this doesn't work for this program. i suspect that i'm just misunderstanding the concept of EOF, but i have no idea what to do or what to search for (so forgive me if this has been answered before).
sorry again for a beginner's unknowingly bad wording. thanks for the help in advance.