In C how is this parameter declared in the function? -
i trying learn basics of c using the c programming language - brian kernighan , dennis ritchie
in program below, don't understand value 'maxlinelength
' comes from?
the loop set run while 'i
' smaller maxlinelength-1
, value of maxlinelength
, come from?
from understand, when parameters declared in function value being passed them, must surely declared somewhere else have 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 under 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; }
from understand, when parameters declared in function value being passed them, must surely declared somewhere else have value passed in?
in case, function being declared , defined @ same time. pre-ansi c allowed that. considered bad practice today. should add forward declaration
int getlinelength(char line[], int maxlinelength);
above main
in order avoid declaring function implicitly.
when forward declaration missing, first use of function becomes implicit declaration. compiler takes types of expression parameters pass, , assumes function takes corresponding types parameters. assumes function returns int
. in case compiler has information necessary figure out correct signature, function defined correctly. however, in more complex cases (say, if maxlinelength
declared long long
, not int
) lead undefined behavior, should include forward declaration of functions.
Comments
Post a Comment