gpt4 book ai didi

c - 字符串在 C 中无法正确打印

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:46 24 4
gpt4 key购买 nike

我在独立函数中使用 while 循环打印字符串时遇到问题。

我有以下代码:

#include <stdio.h>
int pword(char *);
int main() {
char s[] = "Alice";
pword(s);
return 0;
}

int pword(char *s) {
while(*s!='\0') {
printf("%s", s);
s++;
}
printf("\n");
return 0;
}

这是打印:Aliceliceicecee

最佳答案

您每次都打印偏移 单词,而不是字符。

尝试改变(例如)

printf("%s", s);

通过

printf("%c", *s);

或者因为你真的不需要格式化,使用

putchar(*s);

(所有这些意味着您基本上是用循环重写 puts。因此,如果不需要对字符进行进一步处理,也许您应该坚持使用标准函数)

关于c - 字符串在 C 中无法正确打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855984/

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