I am trying to learn the basics of C using The C Programming Language - Brian Kernighan and Dennis Ritchie
我正在尝试使用C编程语言学习C的基础知识--Brian Kernighan和Dennis Ritchie
In the program below, I don't understand where the value for 'maxlineLength
' comes from?
在下面的程序中,我不明白'maxlineLength'的值来自哪里?
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?
for循环设置为在'i'小于maxLineLength-1时运行,但是maxLineLength的值是什么?它来自何处?
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;
}
5 个解决方案
#1
5
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?
根据我的理解,当在这样的函数中声明参数时,一个值被传递给它们,所以必须在其他地方声明它们以传递值?
In this case, the function is being declared and defined at the same time. Pre-ANSI C allowed that. This is considered a bad practice today. You should add a forward declaration
在这种情况下,函数正在同时声明和定义。预ANSI C允许这样做。这被认为是今天的坏习惯。你应该添加一个前向声明
int getLineLength(char line[], int maxLineLength);
above main
in order to avoid declaring the function implicitly.
以上为主要内容,以避免隐式声明该函数。
When this forward declaration is missing, the first use of the function becomes its implicit declaration. The compiler takes types of the expression parameters that you pass, and assumes that the function takes corresponding types as its parameters. It also assumes that the function returns an int
. In this case the compiler has all the information necessary to figure out the correct signature, so the function is defined correctly. However, in more complex cases (say, if maxLineLength
was declared as long long
, not int
) it would lead to undefined behavior, so you should always include a forward declaration of your functions.
当缺少此前向声明时,该函数的第一次使用将成为其隐式声明。编译器接受您传递的表达式参数的类型,并假定该函数将相应的类型作为其参数。它还假定函数返回一个int。在这种情况下,编译器具有确定正确签名所需的所有信息,因此正确定义了函数。但是,在更复杂的情况下(例如,如果maxLineLength被声明为long long,而不是int),则会导致未定义的行为,因此您应该始终包含函数的前向声明。
#2
3
maxLineLength
is a parameter/argument for the getLineLength
function. You are expected to pass in a max length for the line when calling this function.
maxLineLength是getLineLength函数的参数/参数。调用此函数时,您应该传入该行的最大长度。
In the main
function, it's done here:
在main函数中,它在这里完成:
getLineLength(line, MAXIMUMLINELENGTH)
He passes in MAXIMUMLINELENGTH
as the max line length, which is equal to 1000
as can be seen by this definition in the code:
他传入MAXIMUMLINELENGTH作为最大行长度,等于1000,如代码中的定义所示:
#define MAXIMUMLINELENGTH 1000
These #define
s are specific to the preprocessor. It replaces all instances of MAXIMUMLINELENGTH
with 1000
before compiling.
这些#defines特定于预处理器。它在编译之前用1000替换MAXIMUMLINELENGTH的所有实例。
#3
2
The line
这条线
#define MAXIMUMLINELENGTH 1000
is the clue to the puzzle
是这个难题的线索
#4
2
getLineLength(line, MAXIMUMLINELENGTH)
MAXLINELENGTH
is defined constant
MAXLINELENGTH定义为常数
Inside function getLineLength
is this value "mapped" to maxLineLength
variable
内部函数getLineLength是“映射”到maxLineLength变量的值
#5
2
C source files are pre-processed prior to compilation.
在编译之前预处理C源文件。
The #define MAXIMUMLINELENGTH 1000
is a C preprocessor directive that instructs the pre processor to replace all occurrences of the string MAXIMUMLINELENGTH
with 1000
.
#define MAXIMUMLINELENGTH 1000是一个C预处理器指令,指示预处理器将所有出现的字符串MAXIMUMLINELENGTH替换为1000。
If you use MAXIMUMLINELENGTH
consistently through out this compilation unit (.C file) and decide at a later time that the line length should change you only have to make the change to pre-preprocessor directive and then that change is reflected through out this compilation unit.
如果在整个编译单元(.C文件)中一致地使用MAXIMUMLINELENGTH,并在稍后决定行长度应该更改,则只需要对预处理程序指令进行更改,然后通过此编译单元反映该更改。
So the line getLineLength(line, MAXIMUMLINELENGTH)
is transformed by the preprocessor to getLineLength(line, 1000)
所以getLineLength行(line,MAXIMUMLINELENGTH)由预处理器转换为getLineLength(line,1000)
#1
5
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?
根据我的理解,当在这样的函数中声明参数时,一个值被传递给它们,所以必须在其他地方声明它们以传递值?
In this case, the function is being declared and defined at the same time. Pre-ANSI C allowed that. This is considered a bad practice today. You should add a forward declaration
在这种情况下,函数正在同时声明和定义。预ANSI C允许这样做。这被认为是今天的坏习惯。你应该添加一个前向声明
int getLineLength(char line[], int maxLineLength);
above main
in order to avoid declaring the function implicitly.
以上为主要内容,以避免隐式声明该函数。
When this forward declaration is missing, the first use of the function becomes its implicit declaration. The compiler takes types of the expression parameters that you pass, and assumes that the function takes corresponding types as its parameters. It also assumes that the function returns an int
. In this case the compiler has all the information necessary to figure out the correct signature, so the function is defined correctly. However, in more complex cases (say, if maxLineLength
was declared as long long
, not int
) it would lead to undefined behavior, so you should always include a forward declaration of your functions.
当缺少此前向声明时,该函数的第一次使用将成为其隐式声明。编译器接受您传递的表达式参数的类型,并假定该函数将相应的类型作为其参数。它还假定函数返回一个int。在这种情况下,编译器具有确定正确签名所需的所有信息,因此正确定义了函数。但是,在更复杂的情况下(例如,如果maxLineLength被声明为long long,而不是int),则会导致未定义的行为,因此您应该始终包含函数的前向声明。
#2
3
maxLineLength
is a parameter/argument for the getLineLength
function. You are expected to pass in a max length for the line when calling this function.
maxLineLength是getLineLength函数的参数/参数。调用此函数时,您应该传入该行的最大长度。
In the main
function, it's done here:
在main函数中,它在这里完成:
getLineLength(line, MAXIMUMLINELENGTH)
He passes in MAXIMUMLINELENGTH
as the max line length, which is equal to 1000
as can be seen by this definition in the code:
他传入MAXIMUMLINELENGTH作为最大行长度,等于1000,如代码中的定义所示:
#define MAXIMUMLINELENGTH 1000
These #define
s are specific to the preprocessor. It replaces all instances of MAXIMUMLINELENGTH
with 1000
before compiling.
这些#defines特定于预处理器。它在编译之前用1000替换MAXIMUMLINELENGTH的所有实例。
#3
2
The line
这条线
#define MAXIMUMLINELENGTH 1000
is the clue to the puzzle
是这个难题的线索
#4
2
getLineLength(line, MAXIMUMLINELENGTH)
MAXLINELENGTH
is defined constant
MAXLINELENGTH定义为常数
Inside function getLineLength
is this value "mapped" to maxLineLength
variable
内部函数getLineLength是“映射”到maxLineLength变量的值
#5
2
C source files are pre-processed prior to compilation.
在编译之前预处理C源文件。
The #define MAXIMUMLINELENGTH 1000
is a C preprocessor directive that instructs the pre processor to replace all occurrences of the string MAXIMUMLINELENGTH
with 1000
.
#define MAXIMUMLINELENGTH 1000是一个C预处理器指令,指示预处理器将所有出现的字符串MAXIMUMLINELENGTH替换为1000。
If you use MAXIMUMLINELENGTH
consistently through out this compilation unit (.C file) and decide at a later time that the line length should change you only have to make the change to pre-preprocessor directive and then that change is reflected through out this compilation unit.
如果在整个编译单元(.C文件)中一致地使用MAXIMUMLINELENGTH,并在稍后决定行长度应该更改,则只需要对预处理程序指令进行更改,然后通过此编译单元反映该更改。
So the line getLineLength(line, MAXIMUMLINELENGTH)
is transformed by the preprocessor to getLineLength(line, 1000)
所以getLineLength行(line,MAXIMUMLINELENGTH)由预处理器转换为getLineLength(line,1000)