cgcccharacter

Multi-character character constant [-Wmultichar] Error in C


#include <stdio.h>
main ()
{
    int c;
    while ((c = getchar()) != EOF ) {
        if (c == '\t') {
            while ((c = getchar()) == '\t');
                putchar ('\\t');
        }
        else (c == '\b') {
            while ((c = getchar()) == '\b');
                putchar ("\\b");
        }
        else (c == '\\' ) {
            while ((c = getchar()) == '\\');
                putchar ("\\");
        }
        putchar(c);
    }
}

I get the following error when trying to compile:

cpytbb.c: In function ‘main’:
cpytbb.c:8:14: warning: multi-charactercharacter constant [-Wmultichar]
cpytbb.c:10:20: error: expected‘;’ before ‘{’ token

Please note that the second error is probably irrelevant to the issue. I'm new and I most likely have done some mistakes in my code.

In case it's needed, I'm using gcc.

Edit: I'm trying 'print' out \t \b and \ as simple text. As an example if I hit 'backspace' it will print \b


Solution

  • Several issues:

    1. Use of putchar() to try and print more than one character. A backslash is a character, so is a b, or a t.

    2. Use of different quotes on the call to putchar(). Single quotes are for single characters (an escape sequence like \b expands to a single character, so is OK, whereas \\b is an escape sequence for a backslash followed by another character), double quotes are for strings (i.e. a series of zero or more characters ended by a zero-value character (\0).

    3. Even if you fix those, your program will be wrong, because you have a semicolon after your while loop. This means that you have an empty loop (it just performs an "empty statement" each time through the loop) and then after that loop it calls putchar exactly once.

    4. Two else blocks, with conditions. else can not have a condition. You're probably looking to write else if( ... ) instead.