Skip to content

Instantly share code, notes, and snippets.

@Bevilacqua
Created August 3, 2012 20:24
Show Gist options
  • Save Bevilacqua/3251218 to your computer and use it in GitHub Desktop.
Save Bevilacqua/3251218 to your computer and use it in GitHub Desktop.
The C Language: 1.9 Confusing function copy();
/*I copied this word for word with the intentions of interpreting it later but i can't figure out what the function copy is doing!*/
#include <stdio.h>
#define MAXLINE 1000 /*Maximum Input Line Length*/
int getline(char s[],int lim);
void copy(char to[],char from[]);
int main() {
int len; /*Current line length*/
int max; /*maximum length seen so far*/
char line[MAXLINE]; /*current input line*/
char longest[MAXLINE]; /*longest line saved here*/
max = 0;
while((len = getline(line,MAXLINE)) > 0)
if(len > max) {
max = len;
copy(longest,line);
}
if (max > 0) /*there was a line*/
printf("\nThe longest string was:\n%s",longest);
return 0;
}
int getline(char s[],int limit) {
int c,i;
for (i = 0; i < limit - 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 = 0;
while((to[i] = from [i]) != '\0')
++i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment