Mail Archives: djgpp/2000/10/26/12:31:39
In an article posted to usenet, Hans-Bernhard Broeker
<broeker AT physik DOT rwth-aachen DOT de> inscribed the following:
>Kenneth Lantrip <bothersome AT mindspring DOT com> wrote:
>> Looks kinda funny to reply to my own message but... here goes :)
>Esp. if one considers that this one is written with a completely other
>user name on it :-P
I forgot to change my name back... Not real sure how safe using my real name.
>As a side issue: I suggest you take this over to comp.lang.c or some
>other general C newsgroup. This is in no way specific to DJGPP, so it
>doesn't really belong into this newsgroup.
>> #include <stdio.h>
>Somewhere around here, you should add prototype declarations for your
>functions Bin2Int and Int2Bin, so the compiler knows what parameter
>types they take, and what their result type is.
OK, I see more study involved.
>> main(void) {
>[Nitpick: make that "int main(void)". Not specifying the return type
>at all is deprecated usage of C, and is no longer tolerated by the new
>C99 language definition.
OK, I'll start doing that. That was something I wasn't aware of.
>> int x;
>> static unsigned char y[33];
>You don't need that 'static', in this case.
>> x = 42;
>> printf(" %s ", Int2Bin(x, &y));
>> /* someone tell my why that worked!!! */
>You got lucky, that's way. This is seriously incorrect code, which
>invokes what the C language definition calls 'undefined behaviour',
>i.e. *anything* can happen. In this case, the expected thing happened,
>by sheer luck.
>Had you compiled with a proper set of warning options ("gcc -O2 -Wall
>-W" is what I would suggest), you'ld have got a warning about this.
>Correct code would've looked like this:
>
> Int2Bin(x, y);
> printf(" %s ", y);
That's what I had started with, but after experimenting with what works and
what doesn't, It just looked better "the wrong way".
>> strcpy(y, "1011101011");
>
>> printf(" %d \n", Bin2Int(y));
>You don't have to write Bin2Int yourself. There's a standard function,
>'strtol' that does it for you.
Isn't that the way it always works?!? :)
>> }
>> int Int2Bin(int x, char *y) {
>> int i;
>>
>> y += 32; *y-- = 0;
>> for (i = 0; i < 32; i++) {
>> *y-- = 48 + (x & 1);
>> x >>= 1;
>> }
>Bad coding: the promised integer is not return()ed, here. It's
>probably better to return char *, anyway.
Ahhh... Good to know this.
>> }
I appreciate your help... I've got 4 books on the subject, and none of them
had these jewels of info in them. But then I don't read them from front to
back either.
- Raw text -