【问题标题】:Printing a string in C using a function使用函数在 C 中打印字符串
【发布时间】:2010-09-25 16:46:03
【问题描述】:

我是 C 的新手,正在尝试学习如何获取字符串并使用函数打印它。我到处都看到使用while(ch = getchar(), ch >= 0) 的示例,但是一旦我将它放入一个函数(而不是 main()),它就停止工作了。现在,它陷入了无限循环……这是为什么呢?

// from main():
// printString("hello");

void printString(char *ch)
{
    while (*ch = getchar(), *ch >= 0)
    putchar(*ch);
}

【问题讨论】:

  • 我们不能使用 printf 有什么原因吗?
  • getchar() 从标准输入读取输入。你想要那个,还是要打印“hello”?
  • 我实际上将通过将竖线分隔的文件解析为 15 个字符的列来操作数据,但我想将代码从 main 中提取出来并放入一个函数中,而我是只是对如何从标准输入循环遍历字符串感到窒息。

标签: c string char getchar putchar


【解决方案1】:

getchar() 从标准输入读取用户输入。如果要打印传入的字符串,则不需要getchar()

让我们一步一步来。您的循环每次从标准输入读取一个字符,直到它到达文件结尾。这就是ch >= 0 测试检查的内容:只要我们得到有效字符,就继续阅读。对于打印字符串的字符,条件会发生变化。现在一个有效的字符是任何非 NUL ('\0')。所以我们将循环条件更改为:

while (*ch != '\0')

接下来是找出循环体。 putchar(*ch) 很好;我们会把它留在那里。但是如果没有getchar(),我们必须弄清楚“获取下一个字符”的等效语句是什么。

那就是ch++。这会将ch 指针推进到字符串中的下一个字符。如果我们把它放在循环的末尾,那么我们将打印一个字符,前进一个空格,然后检查下一个字符是否为非 NUL。如果是,那么我们将其打印、推进并检查。

while (*ch != '\0') {
    putchar(*ch);
    ch++;
}

【讨论】:

  • 虽然这是正确的,但它不是惯用的。大多数人会将*ch != '\0'简化为*ch,意思完全一样;并且也可以在单个命令中编写主体,putchar(*ch++); - 如果要掌握 C 中 ++ 的行为,理解为什么这是可能的至关重要。
【解决方案2】:

这里发生的情况如下:

  1. 在函数main 中调用printString 并带有指向字符串“hello”的指针
  2. printString 函数尝试读取带有getchar() 的字符
  3. 并将该字符保存在“h”的位置

语言的规则说,试图改变“h”是未定义的行为。如果你幸运的话,你的程序会崩溃;如果你很不走运,它会显示该程序有效。

简而言之:getchar()用于阅读; putchar() 用于写作。

而你想写 5 个字母:'h'、'e'、'l'、'o' 和另一个 'o'。

你好 ^ ch 是一个指针 ch *ch is 'h' -- ch 指向一个 'h'

最后一个“o”之后有什么东西吗? 有!'\0'。零字节终止字符串。所以试试这个(printString("hello");)...

void printString(char *ch)
{
    putchar(*ch); /* print 'h' */
    ch = ch + 1;  /* point to the next letter. */
                  /* Note we're changing the pointer, */
                  /* not what it points to: ch now points to the 'e' */
    putchar(*ch); /* print 'e' */
    ch = ch + 1;  /* point to the next letter. */
    putchar(*ch); /* print 'l' */
    ch = ch + 1;  /* point to the next letter. */
    putchar(*ch); /* print 'l' */
    ch = ch + 1;  /* point to the next letter. */
    putchar(*ch); /* print 'o' */
    ch = ch + 1;  /* point to the next letter. What next letter? The '\0'! */
}

或者您可以在循环中编写它(并使用不同的参数从 main 调用)...

void printString(char *ch)
{
    while (*ch != '\0')
    {
        putchar(*ch); /* print letter */
        ch = ch + 1;  /* point to the next letter. */
    }
}

【讨论】:

    【解决方案3】:

    根据您的描述,您只需要:

    void printString(char *ch)
    {
      while(*ch) {
         putchar(*ch);
         ch++;
      }
    }
    

    你原来的函数:

    void printString(char *ch)
    {
        while (*ch = getchar(), *ch >= 0)
        putchar(*ch);
    }
    

    做了很多事情:

    1. 从标准输入读取字符
    2. 存储从标准输入读取的字符 进入ch 指向的第一个字符(如果您传入字符串文字,这甚至可能不起作用。
    3. 将字符写入标准输出。
    4. 在读取的字符

    【讨论】:

    • 不过,可能没有缺少的大括号。
    • 当读取字符 时也终止
    • @user411313 不,当读取的字符小于0(
    【解决方案4】:

    我只会做printf("%s",str);puts(str);

    【讨论】:

    • puts。不要用大象枪杀死跳蚤。
    • 它必须在一个函数中完成,因为该函数将实际格式化字符串...抱歉,如果我没有正确解释。
    • @Ben Voigt:他不一样。 puts 无条件在末尾添加\n。你可能想要它,也可能不想要它。
    • @Andrey:我想我用得还不够……反正fputs既没有添加尾随换行符,也没有浪费时间解析任何格式字符串,所以这样最好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    相关资源
    最近更新 更多