Skip to content

Commit 1e559dc

Browse files
committed
change longest_line to use globals *shudder*
1 parent 9117030 commit 1e559dc

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

Ch1_Tutorial_Introduction/print_longest_line.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@
22

33
#define MAXLINE 1000 //maximum input line size
44

5-
int get_line( char line[], int maxline );
6-
void copy( char to[], char from[] );
5+
int max;
6+
char line[MAXLINE];
7+
char longest[MAXLINE];
8+
9+
int get_line(void);
10+
void copy(void);
711

812
//print longest input line
913

1014
int main()
1115
{
1216
int len = 0; //current line length
13-
int max = 0; //maximum line length seen
14-
char line[MAXLINE]; //current input line
15-
char longest[MAXLINE]; //longest input line we know about
17+
extern int max;
18+
extern char longest[];
19+
20+
max = 0
1621

17-
while ((len = get_line(line, MAXLINE)) > 0){ //get the line
22+
while ((len = get_line()) > 0){ //get the line
1823
if (len > max){ //if the length is longer than the previous
1924
max = len; //longest, copy it to `longest` and save the length
20-
copy(longest,line);
25+
copy();
2126
}
2227
}
2328
if (max > 0){ //if there was a line, print it
@@ -27,29 +32,32 @@ int main()
2732
}
2833

2934
// read a line in to s, return the length
30-
int get_line(char s[], int lim){
35+
int get_line(void){
3136
int c,i;
37+
extern char line[];
3238

3339
for (i = 0;
34-
i < lim-1
40+
i < MAXLINE-1
3541
&& (c = getchar()) != EOF
3642
&& c != '\n'; ++i){
37-
s[i] = c; //move through the input until you hit a EOF or \n
43+
line[i] = c; //move through the input until you hit a EOF or \n
3844
}
3945
if (c == '\n'){
40-
s[i] = c; //if the line is a single newline char
46+
line[i] = c; //if the line is a single newline char
4147
++i; //increment i
4248
}
43-
s[i] = '\0';
49+
line[i] = '\0';
4450
return i;
4551
}
4652

4753
//copy `from` into `to`, assuming `to` is big enough
4854
//returns nothing (void)
4955
//relies on input being terminated by \0
50-
void copy(char to[], char from[]){
56+
void copy(void){
5157
int i = 0;
52-
while ((to[i] = from[i]) != '\0'){
58+
extern char line[], longest[];
59+
60+
while ((line[i] = longest[i]) != '\0'){
5361
++i;
5462
}
5563
}

0 commit comments

Comments
 (0)