K&R教学可读性差吗?

发布于 2024-09-27 09:30:22 字数 1081 浏览 7 评论 0原文

我已经有一段时间没有看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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女情怀诗 2024-10-04 09:30:22

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.

淡淡離愁欲言轉身 2024-10-04 09:30:22

您不应该期望您的程序能够工作,因为您正在调用未定义的行为

您定义两个特定大小的缓冲区(str 为 11 字节长,str2 为 10 字节长)。然后,在 strcat 期间,您尝试写入不存在的 str[11]。从此时起,程序的执行将不再有任何保证。它可能会崩溃,它可能会执行您所期望的操作,或者可能只是打印“42”并让您想知道为什么。

此外,您不应更改 strcat 中的 *t,因为在较新版本的 C 中,t 的类型为 const char * >。

第三,当重新实现您的环境提供的功能时,请为其指定另一个名称。否则,您的编译器可能会将其替换为一些相当于函数调用的内置代码。例如,GCC 有 __builtin_strlen 有时会取代对 strlen 的调用。

代码的固定版本如下所示:

#include <stdio.h>

/* renamed strcat to str_cat to avoid confusing the compiler */
void str_cat(char *s, const char *t) { /* added the const qualifier to t */

    while (*s++ != '\0');
    s--;
    while (*t != '\0') {
        *s++ = *t++;
    }
    /* removed the needless modification of *t */
    *s = '\0'; /* edit: added this line after the comment from Jonathan Leffler */
}

int main() {
   char str[80] = "Hey, hello"; /* note the large array size here */
   char str2[] = " are you?";

   str_cat(str, str2);
   printf("%s\n", str);

   return 0;

}

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, during strcat, you try to write to str[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 in strcat, since in newer versions of C t has type const 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 to strlen.

The fixed version of the code looks like this:

#include <stdio.h>

/* renamed strcat to str_cat to avoid confusing the compiler */
void str_cat(char *s, const char *t) { /* added the const qualifier to t */

    while (*s++ != '\0');
    s--;
    while (*t != '\0') {
        *s++ = *t++;
    }
    /* removed the needless modification of *t */
    *s = '\0'; /* edit: added this line after the comment from Jonathan Leffler */
}

int main() {
   char str[80] = "Hey, hello"; /* note the large array size here */
   char str2[] = " are you?";

   str_cat(str, str2);
   printf("%s\n", str);

   return 0;

}
巴黎夜雨 2024-10-04 09:30:22

其他更易读、更高效的示例可以通过使用 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 of strlcat and many examples of that source can be found as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文