pluraltx.how
来自「国外网站上的一些精典的C程序」· HOW 代码 · 共 40 行
HOW
40 行
Q: How do the plural_text(), plural_text2(), and plural_text3() macros work?A: OK, it's pretty straightforward. The thing relies on two defined behaviors of standard C:1. Logical comparison operators return 0 for false and 1 for true. Although any non-zero value will test true, logical comparisons are defined to return (int)1.2. A quoted string is simply a shorthand method of defining an anonymous constant array of char whose length is that of the string plus one for the terminating NUL character. Given these facts, let's go through it... The "s" and "es" in the macrosare strings - constant character arrays. Like any character array, you canselect specific elements of the array by subscripting it. In order to formproper plurals, you need to determine if the number is one or something elsesince, in English, only the quantity one is singular (makes sense). Thecomparison to one returns either 1 or 0 which is used to index into thecharacter array. In the case of single-character plurals, it will point toeither the 's' or the terminating NUL. In 2-character plurarls, it ismultiplied by 2 (i.e. shifted left by 1) so it will point to either the 'e'or the terminating NUL. Since an indexed array points to a single element ofthat array, the ampersand is there to take to the address of the characterpointed to. The character plurals also mulitply by two, but the embedded NULcharacter causes either "y" or "ies" for the singular and plural cases. Now, walk through it... If the number is not 1, the comparison will befalse and the zero'th element of the array will be selected. Taking theadrress of the zero'th element of either array will simply point to the wholestring. If the number is exactly 1, the comparison will test true and thereturned address will be that of the terminating NUL in either case.Returning a character pointer to a NUL character is the same as using thestring "", i.e. nothing gets printed. There are alternative ways to express this same algorithm in a macro.Here's one that uses pointer arithmetic rather than array subscripts.#define plural_text(n) "s"+(1==(n))
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?