K&R教学可读性差吗?
我已经有一段时间没有看C了(还在学习),我刚刚又回到了K&R的书里。
我刚刚尝试了练习 5-3 (p107)。
编写我们在第 2 章中展示的函数 strcat 的指针版本:strcat(s,t) 将字符串 t 复制到 s 的末尾。
我想出了这个...
void strcat(char *s, char *t);
void strcat(char *s, char *t) {
while (*s++ != '\0');
s--;
while (*t != '\0') {
*s++ = *t++;
}
*--t = '\0';
}
int main() {
char str[] = "Hey, hello";
char str2[] = " are you?";
strcat(str, str2);
printf("%s\n", str);
return 0;
}
它似乎有效。
我想知道的是,K&R 书经常用尽可能少的行来编写练习 - 我希望他们为上面提供自己的代码示例,你会得到类似 this...
void strcat(char *s, char *t) {
while (*s++ != '\0');
s--;
while ((*s++ = *t++) != '\0');
*--t = '\0';
}
对我来说,这不太可读(也许这个例子不太好,但我经常查看他们的代码并思考如果将其分成几行,我会更好地理解它)。书中提供的示例似乎提倡在循环的条件部分中进行这种分配,实际上每行塞满尽可能多的代码。
即使可读性受到影响,这本书在力所能及的范围内尽力而为,这是否正确?
这只是C 方式吗?
It has been a while since I looked at C (still learning) and I just got back into the K&R book.
I just had a go to Exercise 5-3 (p107).
Write a pointer version of the function strcat that we showed in Chapter 2: strcat(s,t) copies the string t to the end of s.
I came up with this...
void strcat(char *s, char *t);
void strcat(char *s, char *t) {
while (*s++ != '\0');
s--;
while (*t != '\0') {
*s++ = *t++;
}
*--t = '\0';
}
int main() {
char str[] = "Hey, hello";
char str2[] = " are you?";
strcat(str, str2);
printf("%s\n", str);
return 0;
}
It seems to work.
What I am wondering, is that the K&R book often writes exercises with as little lines as possible - I'd expect had they provided their own code sample for above, you'd get things like this...
void strcat(char *s, char *t) {
while (*s++ != '\0');
s--;
while ((*s++ = *t++) != '\0');
*--t = '\0';
}
To me, this is less readable (maybe this example isn't as good, but I often look at their code and think if that was separated into a few lines, I'd understand it much better). The examples provided in the book seem to advocate this sort of assignment in the condition part of a loop, and in fact cramming as much code as possible per line.
Is the book right in trying to do as much possible where you can, even if readability suffers?
Is this just The C Way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
K&R解释了书中习语的重要性。是的,C 程序员很看重代码的简洁性,但故意简洁并不是为了惩罚初学者。经过一段时间的阅读和编写 C 语言后,您开始识别模式,因此当您在别人的代码中看到它们时,您知道自己在看什么。
浏览 K&R 中作为示例给出的
strcpy()
的迭代 - 他们解释了简洁与清晰的哲学,并讨论了习语。K&R explain the importance of idioms in the book. Yes, brevity of code is valued by C programmers, but it's not deliberately terse to punish beginners. After some time reading and writing C you start to recognize patterns, so when you see them in someone else's code you know what you're looking at.
Go through the iterations of
strcpy()
given as an example in K&R -- they explain their philosophy of brevity vs. clarity, and talk about idioms.您不应该期望您的程序能够工作,因为您正在调用未定义的行为。
您定义两个特定大小的缓冲区(
str
为 11 字节长,str2
为 10 字节长)。然后,在strcat
期间,您尝试写入不存在的str[11]
。从此时起,程序的执行将不再有任何保证。它可能会崩溃,它可能会执行您所期望的操作,或者可能只是打印“42”并让您想知道为什么。此外,您不应更改
strcat
中的*t
,因为在较新版本的 C 中,t
的类型为const char *
>。第三,当重新实现您的环境提供的功能时,请为其指定另一个名称。否则,您的编译器可能会将其替换为一些相当于函数调用的内置代码。例如,GCC 有
__builtin_strlen
有时会取代对strlen
的调用。代码的固定版本如下所示:
You should not expect your program to work, since you are invoking undefined behavior.
You define two buffers of a certain size (
str
is 11 bytes long,str2
is 10 bytes long). Then, duringstrcat
, you try to write tostr[11]
, which doesn't exist. From this point on there is no guarantee whatsoever about the execution of your program. It may crash, it may do what you expected, or it might just print "42" and make you wonder why.Furthermore, you should not change
*t
instrcat
, since in newer versions of Ct
has typeconst char *
.And third, when re-implementing a function that is also provided by your environment, give it another name. Otherwise your compiler might replace it with some builtin code that is equivalent to the function call. For example GCC has
__builtin_strlen
which sometimes replaces calls tostrlen
.The fixed version of the code looks like this:
其他更易读、更高效的示例可以通过使用 Google 代码搜索。
请特别查看 Android 和 BSD 的源代码,它们是
strcat
的更现代 C 实现的好示例。您应该编写
strlcat
和 该源的许多示例也可以找到。Other more readable, more efficient examples can be found by using Google Codesearch.
Look at the source code for Android and BSD in particular as good examples of more modern C implementation of
strcat
.Instead of
strcat
you should be writing an implementation ofstrlcat
and many examples of that source can be found as well.