0

我正在学习 C,并且一直在尝试制作一个接受用户输入的程序,并删除其中的任何双空格,然后再次打印出来。我们还没有完成数组,所以我需要一个字符一个字符地做这个。这是我的代码:

#include <stdio.h>

main()
{
    char c;
    int count;
    count = 0;

    while ((c = getchar()) != '\n')
        if (c == ' ')
            count++;
        if (c != ' ')
            count = 0;
        if (count <= 0)
            printf("%s", c);
}

但是,此代码不起作用。编译器返回错误

:15: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

有什么帮助吗?我不知道我做错了什么。

4

3 回答 3

8

使用%c格式说明符打印单个char

printf("%c", c);

%s格式说明符告诉我们printf期待一个以 null 结尾的 char 数组(又名字符串)。

错误消息是指由于参数的默认提升(超出格式字符串)而导致c类型传递给. 这个先前的答案对默认促销有很好的描述;这个先前的线程解释了为什么需要默认提升的一些原因。intprintf

于 2013-01-31T13:15:40.453 回答
0
于 2013-10-11T14:38:36.513 回答
0

您正在使用%swhich 用于字符串,并且需要终止 NULL 字符(\0)..

使用%c将逐字符打印您。

于 2013-01-31T15:18:59.387 回答