1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
// WARNING: This code may not meet the code quality standards.
// You should always use {…} with any if/for/while/switch/do/function.
#define print_counted_noun(noun, count) \
do { \
if(count == 1) { \
printf("%d %s", count, noun); \
} \
else { \
printf("%d %ss", count, noun); \
} \
} while(false)
// LESSON: With multiple statements in the replacement for a #define function-like macro,
// wrap in do { … } while(false) to make them appear as one statement that can have
// a semicolon at the end of it.
int main(int argc, char* argv[]) {
int n = 7;
if(n == 7)
print_counted_noun("lucky slug", 7);
else
print_counted_noun("unlucky slug", 1);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.