Re: pls expand this macro



sathyashrayan wrote:
> /*
> ** 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?

* the macro doesn't know the type of the variable.
if you expand the macro, it looks like this

case 1 / plural_text(0) : &"s"[0]
...
>
> 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"?

* "&" operand will make the right-side variable's pointer
so it makes the array of string which started from the index value
of the array..

>
> 3) << operator in the macro plural_text2 used to move the string to the
> second one, in the above case
> it is "s". Correct?

* << operator shifts the result of "(1==(n))",
so its last value is only be "0" or "2"
"0" will be the "es"
"2" will be the NULL

so you can see the boxes, or box

>
> Can any one explain both of the macros.
> --
> "combination is the heart of chess"
>
> A.Alekhine
>
> Mail to:
> sathyashrayan AT gmail DOT com
>
>

I'm poor at english.

- Park, Sung-jae
.



Relevant Pages

  • Re: Extremly fast dinamic array implementation
    ... It's only an array, for pity's ... Macros don't have a speed. ... foo.c:50: warning: assignment discards qualifiers from pointer target type ...
    (comp.lang.c)
  • Re: Finding number of bits of integer
    ... So of course it does not matter how many bits a single int has, ... Yes, CHAR_BIT gives the number of bits in a char, and a char is exactly one ... The usual way to implement a "bit array", though, is as follows: ... http://www.snippets.org has some macros that can be used for this purpose. ...
    (comp.lang.c)
  • Re: The lack of a boolean data type in C
    ... If I need one, I use an int. ... Who cares about the storage cost of one lousy int? ... I use an array of unsigned char and some bit macros. ...
    (comp.lang.c)
  • Re: Using Arrays In Macro To Find And Replace ?? Help.
    ... That's how the array works - each word is processd individually. ... macros searches pen in the whole doc one by one and replaces it. ... There are different types of pens. ... Dim sRep As VbMsgBoxResult ...
    (microsoft.public.word.vba.general)
  • Naming an Array
    ... a series of Text boxes and objects in front/behind an opaque square using ... simple macros. ... either adding or removing an item, I have to manually change the array ... Is there a way to apply a name to an array that I want to select so I can ...
    (microsoft.public.excel.misc)