elie reveillaud
elie reveillaud

Reputation: 49

problem in c there's an empty character constant error

I've got a problem in C. The console says that there's an empty character constant. It's a little program to know how much books have a person. Here is the code :

int main ()
{
    int nb_books;
    printf("How much books have you got?");
    scanf("%d",&nb_books);
    printf("You have %d book%c!\n",nb_books, (nb_books==0||nb_books==1)?'':'s');
    return 0;
}

The problem is with %c, I'd like to put a s when there are several books. That's all!

Upvotes: 1

Views: 52

Answers (2)

I'm Weverton
I'm Weverton

Reputation: 700

Try this instead:

int main ()
{
    int nb_books;
    printf("How much books have you got?");
    scanf("%d",&nb_books);
    printf("You have %d book%c!\n",nb_books, (nb_books==0||nb_books==1) ? NULL : 's');
    return 0;
}

Upvotes: -3

Remy Lebeau
Remy Lebeau

Reputation: 597540

There is no "empty" character, so '' is illegal.

Use %s instead of %c, then you can use a 0-length string, eg:

printf("You have %d book%s!\n", nb_books, (nb_books==1)?"":"s");

(you should be outputting s for "0 books")

Upvotes: 5

Related Questions