hardcoded constants
Rahul Dhesi
dhesi at bsu-cs.UUCP
Sun Dec 18 01:24:59 AEST 1988
In article <2478 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
>How about sizeof "/"? Or does that return sizeof(char *)?
I considered this and rejected it because it is misleading. We need 2
because one is for a '/' in the middle and one is for a trailing null,
not because we need two for just '/'. That "/" is a string and needs
two is not really the point, even though it gives the right answer.
The format I prefer most is actually:
strlen(a) + 1 + strlen(b) + 1
because it arranges the components of the expression in the right
order. (It also avoids the magic number 2. However, 1 is a magic
number here even though it isn't usually considered to be one.) It
would be nice to be able to say
strlen(a) + sizeof('/') + strlen(b) + 1
but unfortunately sizeof('/') is the same as sizeof(int).
One could also do
#define ONE_CHAR 1
#define TWO_CHARS 2
and then say things like:
strlen(a) + ONE_CHR + strlen(b) + ONE_CHR
strlen(a) + strlen(b) + TWO_CHARS
This sounds like the type of error undergraduates make when they are
first asked to use symbolic constants, but upon thinking I realize that
it could actually clarify the code quite a bit.
--
Rahul Dhesi UUCP: <backbones>!{iuvax,pur-ee}!bsu-cs!dhesi
More information about the Comp.lang.c
mailing list