S
sathyashrayan
/*
** PLURALTX.C - How to print proper plurals
** public domain - original algorithm by Bob Stout
*/
#include <stdio.h>
#define plural_text(n) &"s"[(1 == (n))]
#define plural_text2(n) &"es"[(1 == (n))<<1]
int main(void)
{
int i;
for (i = 0; i < 10; ++i)
printf("%d thing%s in %d box%s\n", i, plural_text(i), i,
plural_text2(i));
return 0;
}
My questions:
1) I try to understand the two macros plural_text and plural_text2.
array subscripting is commutative
so in the above we could expand the macros as plural_text n[1] = "s" .
The parenthesis around
the variable (n) since the macros does not know the type of the variable
it is acting on. Am I correct?
2) I don't able to understand the use of & in both of the macros. Does
it used for the rule "array decays into
the pointer to it's first element"?
3) << operator in the macro plural_text2 used to move the string to the
second one, in the above case
it is "s". Correct?
Can any one explain both of the macros.
--
"combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com
** PLURALTX.C - How to print proper plurals
** public domain - original algorithm by Bob Stout
*/
#include <stdio.h>
#define plural_text(n) &"s"[(1 == (n))]
#define plural_text2(n) &"es"[(1 == (n))<<1]
int main(void)
{
int i;
for (i = 0; i < 10; ++i)
printf("%d thing%s in %d box%s\n", i, plural_text(i), i,
plural_text2(i));
return 0;
}
My questions:
1) I try to understand the two macros plural_text and plural_text2.
array subscripting is commutative
so in the above we could expand the macros as plural_text n[1] = "s" .
The parenthesis around
the variable (n) since the macros does not know the type of the variable
it is acting on. Am I correct?
2) I don't able to understand the use of & in both of the macros. Does
it used for the rule "array decays into
the pointer to it's first element"?
3) << operator in the macro plural_text2 used to move the string to the
second one, in the above case
it is "s". Correct?
Can any one explain both of the macros.
--
"combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com