0

I'm learning C, and have been trying to make a program that takes user input, and removes any double spaces in it, then prints it out again. We have not done arrays yet so I need to do this char by char. This is my code:

#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);
}

This code does not work, however. The compiler returns the error

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

Any help? I have no clue what I am doing wrong.

phuclv
  • 27,258
  • 11
  • 104
  • 360

3 Answers3

8

Use the %c format specifier to print a single char

printf("%c", c);

The %s format specifier tells printf to expect a null-terminated char array (aka a string).

The error message refers to c having type int due to default promotion of arguments (beyond the format string) passed to printf. This previous answer has a nice description of default promotion; this previous thread explains some of the reasoning for why default promotion is necessary.

Community
  • 1
  • 1
simonc
  • 39,997
  • 11
  • 78
  • 100
  • Can you please explain why the error message refers to `int`? – Martin v. Löwis Jan 31 '13 at 13:18
  • @Martinv.Löwis Good point. I've added links to previous threads which contain better explanations than I'd manage – simonc Jan 31 '13 at 13:25
  • Note that `getchar()` returns an `int`, not a `char`. That way you can distinguish a `char` read from `EOF` (can't be a legal `char` value, obviously). Not the brightest interface, but compact (and customary by now). – vonbrand Feb 01 '13 at 01:12
0

You are using %s which is used for a string and which expects a terminating NULL character(\0)..

Using %c will print you char by char..

Raghu Srikanth Reddy
  • 2,629
  • 33
  • 27
  • 39
0

Your code has so many problems

  • First, you print a char with %s (which expects a char pointer, i.e. a string)

    In C, char literals are of type int, so whether promoted or not, they're always int. In C++, char literals would be of type char, but after promotion like other answers said, again they'll be int. A plain char variable will also be promoted to int in expressions, and passed as int in vararg functions like printf. That's why the compiler warns you that argument 2 has type ‘int’, because it's expecting a char* and you're passing it an int

    → You must use %c to print a char

  • Your while loop's body is only the first if block, because in C block scope is defined by {}, not by indentation. So the code will run like this, which is not like what you intended

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

    → You need to put the code block in a pair of brackets. And using else instead of 2 separate ifs to make it more readable and faster (for dumb compilers)

    while ((c = getchar()) != '\n')
    {
        if (c == ' ')
            count++;
        else
            count = 0;
        if (count <= 0)
            printf("%s", c);
    }
    
  • main() is wrong. The correct versions in C would be

    int main(void)
    int main(int argc, char **argv)
    

    See What should main() return in C and C++?

  • c must be declared as int because getchar returns int. See Why must the variable used to hold getchar's return value be declared as int?

A minor point is that instead of int count; count = 0;, just initialize the variable while declaring int count = 0. Or better yet use an unsigned int, because count can't be negative

phuclv
  • 27,258
  • 11
  • 104
  • 360