I am trying to learn the basics of C using The C Programming Language - Brian Kernighan and Dennis Ritchie
In the program below, I don't understand where the value for 'maxlineLength
' comes from?
The for loop is set to run while 'i
' is smaller than maxLineLength-1
, but what is the value of maxLineLength
and where does it come from?
From what I understand, when parameters are declared in a function like this a value is being passed to them, so they must surely be declared somewhere else to have the value passed in?
#include <stdio.h>
#define MAXIMUMLINELENGTH 1000
#define LONGLINE 20
main() {
int stringLength;
char line[MAXIMUMLINELENGTH];
while((stringLength = getLineLength(line, MAXIMUMLINELENGTH)) > 0)
if(stringLength < LONGLINE){
printf("The line was under the minimum length\n");
}
else if (stringLength > LONGLINE){
printf("%s", line);
}
return 0;
}
int getLineLength(char line[], int maxLineLength){
int i, c;
for(i = 0; i< maxLineLength-1 && ((c = getchar())!= EOF) && c != '\n'; i++)
line[i] = c;
if(c == '\n') {
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}