Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This version of printLongestLine prints the longest input line, but is specialized to use external variables. These are external to all functions, and can be accessed by name by any function.
- #include <stdio.h>
- #define MAXLINE 1000 //maximum input line size
- int max; // maximum input line size
- char line[MAXLINE]; // current input line
- char longest[MAXLINE]; // longest line saved here
- int mygetline(void);
- void copy(void);
- // print longest input line;, specialized version
- main()
- {
- int len;
- extern int max;
- extern char longest[];
- max = 0;
- while ((len = mygetline()) > 0) {
- if (len > max) {
- max = len;
- copy();
- }
- }
- if (max > 0)
- printf("%s", longest);
- return 0;
- }
- // mygetline: specialized version
- int mygetline(void)
- {
- int c, i;
- extern char line[];
- for (i = 0; i < MAXLINE -1 && (c = getchar()) != EOF && c != '\n'; ++i)
- line[i] = c;
- if (c == '\n') {
- line[i] = c;
- ++i;
- }
- line[i] = '\0';
- return i;
- }
- // copy: specialized version
- void copy()
- {
- int i;
- extern char line[], longest[];
- i = 0;
- while (longest[i] = line[i] != '\0' )
- ++i;
- }
Add Comment
Please, Sign In to add comment