gpt4 book ai didi

c - 为什么打印一个char数组后有一个10?

转载 作者:行者123 更新时间:2023-11-30 18:16:54 25 4
gpt4 key购买 nike

我想知道,为什么我的打印末尾总是有一个 10。

我试图输入带有空格的字符串,然后打印所有字符。

输入:你好

输出:104 101 108 108 111 10

应输出:104 101 108 108 111

  unsigned char input[256];
fgets(input,sizeof(input),stdin);

for(int i = 0; input[i] != '\0'; i++) {
printf("%u ",input[i]);
}

最佳答案

10 是换行符。 fgets 使用换行符并将其复制到缓冲区。来自 the docs :

Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.

文档是您的 friend 。

关于c - 为什么打印一个char数组后有一个10?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26001909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com