Discuss Decimal to Binary in the C Programming forum on Dev Shed. Decimal to Binary C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 65
Time spent in forums: 13 h 46 m 38 sec
Reputation Power: 0
Decimal to Binary
Im trying to make a program which converts a decimal to binary (without bitwise operators), however I am getting some strange output and can not see what Im doing wrong, any help?
Posts: 3,398
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Quote:
Originally Posted by Neutralise
Output I am getting is:
12 in binary: 11000011
Which is the the correct answer, but also the reverse at the end.
Im not sure why the 'bits' at the end are being added.
That is because you are over-running the buffer. As I pointed out, you only have 4 characters of storage where you are trying to store 5. They just happen to be sequential. You need to turn up the warning level on your compiler. It should not accept the following code:
Posts: 16
Time spent in forums: 16 h 40 m 25 sec
Reputation Power: 0
sorry about earlier
to display it correctly, you can either display each element in each array as a character as one solution or adjust the size of the array.
Posts: 3,398
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Quote:
Originally Posted by MadDogBrown
#define NULL 0
(Do they still do that?)
What is the current convention?
I think they do still do that actually.
The standards language use the phrases null pointer and null character. Here's what the C standard says (as of 2005-05):
Quote:
Originally Posted by n1124.pdf
An integer constant expression with the value 0, or such an expression cast to type
void *, is called a null pointer constant.55) If a null pointer constant is converted to a
pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal
to a pointer to any object or function.
Conversion of a null pointer to another pointer type yields a null pointer of that type.
Any two null pointers shall compare equal
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,389
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 17 m 29 sec
Reputation Power: 4080
Quote:
Originally Posted by MadDogBrown
Am I a "Bad Dog" for referring to ascii nul as NULL?
Certainly that can't be worse than compiler vendors doing:
#define NULL 0
(Do they still do that?)
What is the current convention?
I can see how the usage of NULL can be ambiguous; perhaps "NULL character", or nul is the better way....
The basic idea of NULL is that it is a special value for pointer that is not the address of any function or successful allocation operation. Thus, you can never get NULL for a successful malloc(), calloc(), strdup() etc. and value of &any_function is guaranteed never to be NULL.
Now, NULL doesn't necessarily have to be at memory address 0 on the machine. It could be any address that is guaranteed never to be used by the computer for any legitimate data or code. In fact, there have been a few architectures where the NULL address was pointing somewhere else other than memory address 0.
Now for a final wrinkle, the C standard guarantees that whenever a pointer points to the constant 0
e.g.
int *foo = 0;
char *bar = 0;
etc.
this is converted by the compiler at compile time to point to that special NULL address. As we noted above, the actual address of NULL need not be mem. address 0, but the compiler will compile to code to point to wherever NULL is for that architecture.
Thus, #define NULL 0 will work when you do:
int *foo = NULL;
because the compiler will see that line written as:
int *foo = 0;
after the preprocessor replaces NULL with 0. As per the standard, assigning a constant 0 to a pointer will make the compiler point it to the actual NULL address on that machine.
With that said, it is never a good idea to interchange NULL pointer with the ASCII NUL character ('\0'). They are two different things. In particular, it is not a good idea to use NULL when you want to put '\0' in an array.
__________________ Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
Last edited by Scorpions4ever : December 3rd, 2009 at 11:52 PM.