#DEFINE MAXLINE 1000 /* MAXIMUM INPUT
LINE SIZE */
MAIN() /* FIND LONGEST LINE */
{
INT LEN; /* CURRENT LINE LENGTH */
INT MAX; /* MAXIMUM LENGTH SEEN SO FAR */
CHAR LINE[MAXLINE]; /* CURRENT INPUT LINE */
CHAR SAVE[MAXLINE]; /* LONGEST LINE, SAVED */
MAX = 0;
WHILE ((LEN = GETLINE(LINE, MAXLINE)) > 0)
IF (LEN > MAX) {
MAX = LEN;
COPY(LINE, SAVE);
}
IF (MAX > 0) /* THERE WAS A LINE */
PRINTF("%S", SAVE);
}
GETLINE(S,LIM) /* GET LINE INTO S,RETURN LENGTH */
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(S1, S2) /* COPY S1 TO S2;
ASSUME S2 BIG ENOUGH */
CHAR S1[], S2[];
{
INT I;
I = 0;
WHILE ((S2[I] = S1[I] != '\0')
++I;
}
I have some questions same as compilator. )
1. Why main function is at top in program listing
2. Array LINE and SAVE defined only at function MAIN, how can other function can get access to them.
3. Kernigan tells variables can be pass to fucntion only as value not as link, so how can i alter value of array at any other function except MAIN where it was be defined.
I am pretty sure the book you are using is at least 20 years old. main doesn't have a return value here, the functions are old K&R style functions... this example isn't valid anymore since the C90 standard was released. Which was, as you could probably already guess, in 1990. I don't know who gave you that book, but you should tell him to go a bit with the time.
I don't have a copy of the standard, but if I tell Code Blocks (GCC) to use ANSI C90 it does compile old style function declarations without even a warning. I think they were only deprecated in C99.
New style functions were introduced in C90 though, it would have made sense if they deprecated the old ones then.
Though I gotta admit that I don't have a copy of the standard myself, I doubt anyone would have still taught K&R style after C90, considering you can't create prototypes with them.
#include <iostream>
#include <stdio.h>
#define MAXLINE 100
usingnamespace std;
char line[MAXLINE];
char save[MAXLINE];
int Copy(char line[MAXLINE], char save[MAXLINE]){
int i=0;
externchar line[MAXLINE];
externchar save[MAXLINE];
while (line[i]=save[i]!='\0')
++i;
return 0;
}
int GetLine(char line[MAXLINE],int lim){
int c,i;
externchar line[MAXLINE];
for (i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';++i)
line[i]=c;
if (c=='\n') {
line[i]=c;
++i;
}
line[i]='\0';
return(i);
}
int main () {
int len; //current line length
int max; //max length seen so far
int i;
externchar line[MAXLINE];
externchar save[MAXLINE];
max=0;
while ((len=GetLine(line,MAXLINE))>0)
if (len>max){
max=len;
Copy(line,save);
}
if (max>0) {
for(i=0;i<MAXLINE;i++) {
if (save[i]!='\0') cout <<save[i]<<endl;
}
}
return 0;
} //end of main
it doesn't give any errors, but empty output. (
Can somebody help ?
Thanks at advance
#include <iostream>
#include <stdio.h>
#define MAXLINE 100
usingnamespace std;
char line[MAXLINE];
char save[MAXLINE];
int Copy(char line[MAXLINE], char save[MAXLINE]){
int i=0;
externchar line[MAXLINE];
externchar save[MAXLINE];
while (line[i]!='\n') {
save[i]=line[i];
i++;
}
save[i]='\n';
return 0;
}
int GetLine(char line[MAXLINE],int lim){
int c,i;
externchar line[MAXLINE];
for (i=0;(i<lim-1) && ((c=getchar())!=EOF) && (c!='\n');++i)
line[i]=c;
if (c=='\n') {
line[i]=c;
i++;
}
// line[i]='\t';
return(i);
}
int main () {
int len; //current line length
int max; //max length seen so far
int i;
externchar line[MAXLINE];
externchar save[MAXLINE];
max=0;
while ((len=GetLine(line,MAXLINE))>0)
if (len>max){
max=len;
Copy(line,save);
}
if (max>0) {
cout <<save;
}
return 0;
} //end of main