【问题标题】:putchar() output is a question mark instead entered input streamputchar() 输出是一个问号而不是输入输入流
【发布时间】:2020-11-17 02:03:15
【问题描述】:

我正在尝试在 do-while 循环和何时输出带有字母字符的输入流 用户执行 EOF(ctrl+d) 循环停止,输出应该是输入流,但输出只是框中的问号......什么这里有错吗?

#include <stdio.h>

int main(){
    char c;

    do{
        c = getchar();
    } while (c != EOF);

    printf("Output:\n");
    putchar(c);

    return 0;
}

【问题讨论】:

  • char c 只为一个字符提供存储空间。它不会连接循环中getchar() 返回的所有字符。在循环之后,它具有getchar() 返回的最后一个字符,即EOF。
  • c 值等于 EOF 状态。所以你基本上是在“试图显示 EOF”。
  • putchar(c) 放入循环中。
  • 另外,你需要声明int c;

标签: c do-while eof getchar putchar


【解决方案1】:

getchar 返回一个int,所以你应该将你的 char 设为 int。此外,如果你得到一个EOF 并覆盖c,那么你将在循环之后调用putchar(EOF),这不是一个可打印的字符。 (EOF 是-1,它被转换为char,它变成\xff)。如果你想要输入流,那么你必须存储输入流:

#include <stdio.h>

#define INPSIZE 2000

int main() {
    char inp[INPSIZE + 1];
    int i = 0;
    while (1) {
        int c = getchar();
        if (c == EOF) {
            break;
        }
        inp[i++] = c;
        // don't overflow
        if (i >= INPSIZE) {
            break;
        }
    }
    // we need terminating nullbyte
    inp[i] = '\x00';
    puts("Output:");
    puts(inp);
}

【讨论】:

    【解决方案2】:

    变量 c always 在您退出循环时保存 EOF 状态值。您可以尝试将输入保存在一个数组中,或者更简单的方法,您可以在循环中显示每个字符。

    存储在数组中:

    #include <stdio.h>
    
    #define SIZE 500
    
    int main (void)
    {
      char c;
      char arr[SIZE];
      size_t i = 0U;
    
     while ((c=getchar())!=EOF)
        {
          if (i<SIZE-1) /* Consider you want your output as a string,save space for NULL */
          {
              arr[i++]=c;
          }
          else
          {
              puts("buffer full");
              break;
          }
        }
      
      arr[i]='\0';
      
    
      printf ("Output: %s\n",arr);
      
      return 0;
    }
    

    显示每个字符

    #include <stdio.h>    
    
    int main (void)
    {
      char c;
      
      while ((c=getchar())!=EOF)
      {
          putchar(c);
      }
      
      return 0;
    }
    

    【讨论】:

      【解决方案3】:

      我设法完成了我想要编写代码的方式:

      #include <stdio.h>    
      
      
          int main (void){
          int c;
          char c_arr[500];
          size_t i = 0U;
          
          do{
          
          c_arr[i++] = c = getchar();
          
            }while(c!= EOF);
          
          c_arr[i] = '\0';
          i-=1;
          
          printf("Output:\n");
          
          for(size_t a = 0U ; a < i; a++){
          putchar(c_arr[a]);
          }
      
          return 0;
      }
      

      该代码可以吗?或者是否有任何优化可能?

      还有一个关于 size_t 的问题,它比普通 int 用于数组或 for 循环更好吗,就像我在我的 for 循环中所做的那样,还是没关系?

      感谢所有试图提供帮助的人。

      【讨论】:

      • a) 不是真正的优化......但您可以提高缩进的一致性和空格的使用,检查您是否没有将超过 500 个字符写入数组; b) intsize_t 对您的示例无关紧要,尽管 size_t(或 unsigned)更自然,因为您永远不会有负索引。
      • 我不太明白你在回答 a) 中的意思
      猜你喜欢
      • 2022-01-22
      • 2017-07-03
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多