Code which tacks on an "s" when a described quantity is not exactly 1:
The output:
The expression of interest points to the empty string for quantity 1, and to the string "s" for all other quantities. To suppress a diagnostic message for this particular expression,
Code:
#include <stdio.h>
void display_rabbit_count(int the_count)
{
printf(">>>%d rabbit%s<<<\n",
the_count,
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wstring-plus-int"
"s"+(the_count==1) /* <--------- the expression of interest */
#pragma clang diagnostic pop
);
}
int main(void)
{
display_rabbit_count(0);
display_rabbit_count(1);
display_rabbit_count(2);
display_rabbit_count(3);
return 0;
}
Code:
>>>0 rabbits<<<
>>>1 rabbit<<<
>>>2 rabbits<<<
>>>3 rabbits<<<
clang
requires me to use #pragma. Is there any other C expression, similarly concise, which will do the job and help me avoid the warning in the first place? You'd think that I could follow the advice of the diagnostic message and index into the string, and I'd need to use %c, not %s, in the format string, but the case of one rabbit would produce a NUL character in the output, which I wish to avoid.