字符串在 C 中没有正确打印

MkP*_*Ptr 0 c

在独立函数中使用 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;
}
Run Code Online (Sandbox Code Playgroud)

这是打印:Aliceliceicecee

Jea*_*bre 5

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

尝试改变(例如)

printf("%s", s);
Run Code Online (Sandbox Code Playgroud)

经过

printf("%c", *s);
Run Code Online (Sandbox Code Playgroud)

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

putchar(*s);
Run Code Online (Sandbox Code Playgroud)

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