Re: Promoting unsigned long int to long int



pereges <Broli00@xxxxxxxxx> writes:
On Jun 30, 8:49 pm, Keith Thompson <ks...@xxxxxxx> wrote:
These types already have perfectly good names already. Why give them
new ones?

No purpose really. Just that in some of my functions, there are too
many parameters already and the whole thing doesn't fit in single
line.

So write it on multiple lines.

Given:
#define ulong unsigned long int
#define uchar unsigned char
or, preferably:
typedef unsigned long int ulong;
typedef unsigned char uchar;

If I'm reading your code and see a reference to "ulong", I can't
understand what it means until I've confirmed that "ulong" means
"unsigned long int" (which I'd probably write as "unsigned long").
And I have to wonder whether you might some day change the definition
so "ulong" means something else.

If you drop the definition of "ulong" and just write "unsigned long"
directly, I don't have to wonder; your code will be clearer.

Now if you want a typedef whose name says something about how you're
using the type, rather than how it's define, that's a different
matter.

You use "ulong" for array indices; it would make more sense to use
size_t.

You use "uchar" for a parameter that can only have the value 0, 1, or
2. I'd use int. Using unsigned char might save some space, but it's
just as likely to cost you in code size, since the compiler has to
generate code to expand the 1-byte value into a word before it can
operate on it, and to shrink it back down to 1 byte before storing it.
You also make the reader wonder whether there's some fundamental
reason for this value to fit into a byte (there isn't). If you had an
array of these things, it would make sense to use a smaller type.
Since it's just a single parameter, using int is fine.

If you must rename them for some reason, use typedefs, not macros.


ok

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Promoting unsigned long int to long int
    ... typedef unsigned long int ulong; ... typedef unsigned char uchar; ...
    (comp.lang.c)
  • Re: Promoting unsigned long int to long int
    ... typedef unsigned long int ulong; ... typedef unsigned char uchar; ...
    (comp.lang.c)
  • Re: Is It UB ??
    ... unsigned char + signed char = unsigned char ... unsigned char + signed int = signed int ... You've used a value of unsigned int that _could_ fit into an int, ... but it's well-defined -- i.e. that language standard ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Set all bits to 1 in an array of raw bytes
    ... middle parameter was an int rather than an unsigned char. ... here is that UCHAR_MAX isn't guaranteed to "fit" in an int. ... byte and memset sets bytes, then surely an int will be bigger or ...
    (comp.lang.c)
  • Re: HELP!! Two questions from <>
    ... int main ... If 163 will not fit in a char and since EOF is ... is it even possible for getchar to return ... fits just fine in an unsigned char, so getchar is allowed to return 163. ...
    (comp.lang.c)