Reputation: 49
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
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
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