In C, how is this parameter declared in a function?

I'm trying to learn the basics of C using the C programming language - Brian Kernigan and Dennis Ritchie

In the program below, I don’t understand where the value of 'maxlineLength ' came from?

The for loop is set to run, while ' i ' is less 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 passed to them, so they need to be declared somewhere else in order to pass the value to?

 #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; } 
+6
source share
5 answers

From what I understand, when parameters are declared in a function like this, a value is passed to them, so they need to be declared somewhere else in order to pass the value to?

In this case, the function is declared and defined at the same time. Pre-ANSI C resolved this. Today, this is considered bad practice. You must add an ad ahead

 int getLineLength(char line[], int maxLineLength); 

above main , so as not to declare the function implicitly.

If this declaration is missing, the first use of the function becomes its implicit declaration. The compiler accepts the types of parameters of the expression that you pass, and assumes that the function accepts the corresponding types as its parameters. It also assumes that the function returns int . In this case, the compiler has all the information necessary to determine the correct signature, so the function is defined correctly. However, in more complex cases (say, if maxLineLength was declared as long long , not int ), this will lead to undefined behavior, so you should always include a direct declaration of your functions.

+5
source

maxLineLength - parameter / argument for getLineLength function. It is expected that when you call this function, you must pass the maximum length for the line.

In the main function, this is done here:

 getLineLength(line, MAXIMUMLINELENGTH) 

It passes in MAXIMUMLINELENGTH as the maximum line length, which is 1000 , as can be seen from this definition in the code:

 #define MAXIMUMLINELENGTH 1000 

These #define preprocessor specific. It replaces all instances of MAXIMUMLINELENGTH with 1000 before compilation.

+3
source

Line

 #define MAXIMUMLINELENGTH 1000 

is the key to the puzzle

+2
source
 getLineLength(line, MAXIMUMLINELENGTH) 

MAXLINELENGTH determined by a constant

The getLineLength internal function is the value "mapped" to the MAXLINELENGTH variable

+2
source

C source files are pre-processed before compilation.

#define MAXIMUMLINELENGTH 1000 is a C preprocessor directive that instructs the preprocessor to replace all occurrences of the MAXIMUMLINELENGTH string with 1000 .

If you use MAXIMUMLINELENGTH through this compilation unit (.C file) and decide later that the line length should change, you need to make changes to the preprocessor directive, and then this change will be reflected through this compilation unit.

Thus, the line getLineLength(line, MAXIMUMLINELENGTH) converted by the preprocessor to getLineLength(line, 1000)

+2
source

Source: https://habr.com/ru/post/945350/


All Articles