Newbie - itoa implementation

G

gareth.p.davies

Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related. Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?

I also didn't/don't feel I can implement a 'radix' parameter - so I
left that out.

Thanks,

gareth
 
P

pete

Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related.
Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?


#include <limits.h>

char itoa_buff[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];

3.3 is slightly smaller than (log(10) / log(2))
 
M

MQ

Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related. Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?

I also didn't/don't feel I can implement a 'radix' parameter - so I
left that out.

Thanks,

gareth


You can't pass a pointer to a static char array. It will not work how
you expect it to work. If you make three calls to the function

Eg.
char * a = itoa(6);
char * b = itoa(7);
char * c = itoa(8);

a, b and c will all point to the same array, and all will therefore
print out "8" when printed. That is because you are just getting
pointers to the same static array, which will hold the result of the
latest function invocation. This is how it works on my platform; I
haven't checked to see if this is declared as "undefined behaviour" in
C. However, on a PC/Linux machine, this is what happens.

MQ
 
P

pete

MQ said:
Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

You can't pass a pointer to a static char array.

You can return a pointer to such an array from a function.
It will not work how you expect it to work.

That all depends on what you expect.
If you make three calls to the function

Eg.
char * a = itoa(6);
char * b = itoa(7);
char * c = itoa(8);

a, b and c will all point to the same array, and all will therefore
print out "8" when printed. That is because you are just getting
pointers to the same static array, which will hold the result of the
latest function invocation.

However, you can copy the contents of the array
to a distinct somewhere else,
each time that the function is called.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <limits.h>

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

int main(void)
{
char a[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];
char b[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];
char c[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];

strcpy(a, itoa(6));
strcpy(b, itoa(7));
strcpy(c, itoa(8));
puts(a);
puts(b);
puts(c);
return 0;
}

/* END new.c */
 
H

Harald van =?UTF-8?B?RMSzaw==?=

pete said:
MQ said:
Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

You can't pass a pointer to a static char array.

You can return a pointer to such an array from a function.
It will not work how you expect it to work.

That all depends on what you expect.
If you make three calls to the function

Eg.
char * a = itoa(6);
char * b = itoa(7);
char * c = itoa(8);

a, b and c will all point to the same array, and all will therefore
print out "8" when printed. That is because you are just getting
pointers to the same static array, which will hold the result of the
latest function invocation.

However, you can copy the contents of the array
to a distinct somewhere else,
each time that the function is called.

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <limits.h>

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

int main(void)
{
char a[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];
char b[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];
char c[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];


A question: is that valid C89, too? gcc accepts it
with -std=c89 -pedantic-errors, but I was under the impression that integer
constant expressions could not contain floating point arithmetic.
 
P

pemo

MQ said:
Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related. Where I #define BUFFSIZE, I'm not sure that this is good -
for example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a
problem, if so, how can I avoid that, e.g., just use a const instead?

I also didn't/don't feel I can implement a 'radix' parameter - so I
left that out.

Thanks,

gareth


You can't pass a pointer to a static char array. It will not work how
you expect it to work. If you make three calls to the function

Eg.
char * a = itoa(6);
char * b = itoa(7);
char * c = itoa(8);

a, b and c will all point to the same array, and all will therefore
print out "8" when printed. That is because you are just getting
pointers to the same static array, which will hold the result of the
latest function invocation. This is how it works on my platform; I
haven't checked to see if this is declared as "undefined behaviour" in
C. However, on a PC/Linux machine, this is what happens.


The OP is overwriting whatever was there on subsequent calls.


This works for me ...

6
7
8
7
6


#include <stdio.h>

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}


int main(void)
{
puts(itoa(6));
puts(itoa(7));
puts(itoa(8));
puts(itoa(7));
puts(itoa(6));

return 0;
}
 
M

MQ

pete said:
However, you can copy the contents of the array
to a distinct somewhere else,
each time that the function is called.

sounds like a poor design to me. Simply passing the char array as a
parameter would be neater. This pattern exists for such library
functions as fread()

MQ
 
G

gareth.p.davies

pete said:
Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related.
Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?


#include <limits.h>

char itoa_buff[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];

3.3 is slightly smaller than (log(10) / log(2))


Thanks Pete.

But, can you break down the maths a bit for me please, e.g., what's the
3.3 for (and the + 3) for/derived from? I see you're getting 3.3 from
log(10) / log(2), but I don't understand why this is done, apart from
thinking it's something to do with something to do with denary and
binary??

By the way,

(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3

has the value 12 on my system - so I'm assuming you're saying that
MAX_INT has at most 12 character places? It doesn't on my system,
e.g., I have 10.

#define INT_MAX 2147483647

Sure I'm missing heaps here though!

many thanks

gareth
 
P

pemo

MQ said:
Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related. Where I #define BUFFSIZE, I'm not sure that this is good -
for example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a
problem, if so, how can I avoid that, e.g., just use a const instead?

I also didn't/don't feel I can implement a 'radix' parameter - so I
left that out.

Thanks,

gareth


You can't pass a pointer to a static char array. It will not work how
you expect it to work. If you make three calls to the function

Eg.
char * a = itoa(6);
char * b = itoa(7);
char * c = itoa(8);

a, b and c will all point to the same array, and all will therefore
print out "8" when printed. That is because you are just getting
pointers to the same static array, which will hold the result of the
latest function invocation. This is how it works on my platform; I
haven't checked to see if this is declared as "undefined behaviour" in
C. However, on a PC/Linux machine, this is what happens.


Oh, sorry, just read what you wrote again - and yes,

printf("%s %s %s\n", a, b, c);

will result in 8 8 8
 
A

Ancient_Hacker

Here's one that handles negative numbers:

#define BUFFSIZE 30

#define pc(c) ( 12345 == (*p99++ = c))

char * p99; char s99[BUFFSIZE];

void putd( int aa ){ if( aa > 9 ) putd( aa / 10 ); pc( (char) (aa %
10) + '0' ); }

char * ita( int a ) { p99 = s99; putd( a < 0 ? pc( '-' ) - a : a
);return s99 + pc( '\0' );
}


int main( int argc, char * argv[] )
{ char foo[1000]; int i; char * s; );
for( i = -123; i < 123 ; i ++ ) { s = ita( i ); printf( "%d = '%s'\n",
i, s ); }
gets_s( foo );
return 0;
}
 
S

Simon Biber

pete said:
#include <limits.h>

char itoa_buff[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];

3.3 is slightly smaller than (log(10) / log(2))

Thanks Pete.

But, can you break down the maths a bit for me please, e.g., what's the
3.3 for (and the + 3) for/derived from? I see you're getting 3.3 from
log(10) / log(2), but I don't understand why this is done, apart from
thinking it's something to do with something to do with denary and
binary??

sizeof(int) * CHAR_BIT is the number of bits in int.
One is taken for the sign bit, so subtract one bit.

eg. 4 * 8 - 1 == 31 ( where INT_MAX is 2^31 - 1)

Now we need to convert from number of binary (base 2) digits to number
of decimal (base 10) digits. You can do that by scaling the value by
log(2) / log(10), where 2 is the base from, and 10 is the base to.

eg. 31 bits * log(2) / log(10) = 9.33 digits

Since we can't have a fractional number of digits, we must add one so it
rounds up to the correct integer (10 in the example).

Since we need space for the minus sign, we must add one again.

Since we need space for the null character to terminate the string, we
must add one again.

That's why the plus three.
By the way,

(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3

has the value 12 on my system - so I'm assuming you're saying that
MAX_INT has at most 12 character places? It doesn't on my system,
e.g., I have 10.

#define INT_MAX 2147483647

But you absolutely do need 12 bytes: 10 digits, a minus sign and a null
character to terminate the string.
 
F

Frederick Gotham

S

Snis Pilbor

MQ said:
sounds like a poor design to me. Simply passing the char array as a
parameter would be neater. This pattern exists for such library
functions as fread()

Boy am I glad I don't work under you, since you seem to think you're
the sole arbiter of good design.

Example: In one project I work on, very frequently we need to
capitalize one string in a sprintf, but only one (whose length is
bounded). For this we use a capitalize function which returns a
pointer to a static buffer. Other options would be:

1. pass in a buffer like you suggest; but this would add probably 500
lines of declaring buffers all over the damn place, and accomplish
nothing.

2. have capitalize return malloc'd memory, to be freed by caller.
Again, this would add probably 500 lines, and be extremely vulnerable
to accidental memory leaking

3. have capitalize return malloc'd memory and keep track of it in a
table which is periodically free'd in an updater utility function.
This would be a lot of additional work for very little gain. I may as
well switch to a language with built in garbage management and fancy
string libraries, since I've just completely forfeited the whole
benefit of doing it manually!

Or, just remember that capitalize is not reentrant.

If I wanted a language which was always like "Hey! That's, um...
questionable, because you might not know what you're doing... so um,
it's forbidden!" then I would switch to Java.

Also the fact you suggested returning a pointer to static memory might
be undefined behavior, is extremely misleading and probably adds new
lots of new confusion to anyone who hasn't been doing C long enough to
immediately see it as the nonsense it is.
 
A

Andrew Poelstra

pete said:
Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?

#include <limits.h>

char itoa_buff[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];

3.3 is slightly smaller than (log(10) / log(2))

Thanks Pete.

But, can you break down the maths a bit for me please, e.g., what's the
3.3 for (and the + 3) for/derived from? I see you're getting 3.3 from
log(10) / log(2), but I don't understand why this is done, apart from
thinking it's something to do with something to do with denary and
binary??

By the way,

(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3

has the value 12 on my system - so I'm assuming you're saying that
MAX_INT has at most 12 character places? It doesn't on my system,
e.g., I have 10.

#define INT_MAX 2147483647

Sure I'm missing heaps here though!

(size_t) simply converts this to a unsigned type capable of indexing
any array imaginable, according to the Standard. And by "imaginable",
I mean "guaranteed to be conforming.

(sizeof (int) * CHAR_BIT) is the number of bits in an integer type. -1
drops it down because we don't need to count the sign bit.

Now, to get the maximum number of digits, we want log_10 (2^num_bits).

Since log_n (2 ^ x) = x * log_n 2,
log_10 (2 ^ num_bits) = num_bits * log_10 (2);

Since that number is small and prone to precision loss, we instead divide
by the recipricol. (I'm not sure why pete said log(10) instead of just 1.)

Since essentially did an integer division by 4. (That is, in the worst case
that's the equivilant), we need to add 3 to compensate for the potential
loss.

Kudos to pete for coming up with that; Took me 10 minutes to figure out.
 
A

Andrew Poelstra

pete said:
(e-mail address removed) wrote:
Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?

#include <limits.h>

char itoa_buff[(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3];

3.3 is slightly smaller than (log(10) / log(2))

Thanks Pete.

But, can you break down the maths a bit for me please, e.g., what's the
3.3 for (and the + 3) for/derived from? I see you're getting 3.3 from
log(10) / log(2), but I don't understand why this is done, apart from
thinking it's something to do with something to do with denary and
binary??

By the way,

(size_t)((sizeof(int) * CHAR_BIT - 1) / 3.3) + 3

has the value 12 on my system - so I'm assuming you're saying that
MAX_INT has at most 12 character places? It doesn't on my system,
e.g., I have 10.

#define INT_MAX 2147483647

Sure I'm missing heaps here though!

(size_t) simply converts this to a unsigned type capable of indexing
any array imaginable, according to the Standard. And by "imaginable",
I mean "guaranteed to be conforming.

(sizeof (int) * CHAR_BIT) is the number of bits in an integer type. -1
drops it down because we don't need to count the sign bit.

Now, to get the maximum number of digits, we want log_10 (2^num_bits).

Since log_n (2 ^ x) = x * log_n 2,
log_10 (2 ^ num_bits) = num_bits * log_10 (2);

Since that number is small and prone to precision loss, we instead divide
by the recipricol. (I'm not sure why pete said log(10) instead of just 1.)

Since essentially did an integer division by 4. (That is, in the worst case
that's the equivilant), we need to add 3 to compensate for the potential
loss.

Completely wrong on why we're adding 3:
+1 since we'll have a fractional number and we must round up.
+1 for the null-termination
+1 for a potential minus sign.
 
F

Frederick Gotham

Andrew Poelstra posted:
(size_t) simply converts this to a unsigned type capable of indexing
any array imaginable, according to the Standard. And by "imaginable",
I mean "guaranteed to be conforming.

(sizeof (int) * CHAR_BIT) is the number of bits in an integer type. -1
drops it down because we don't need to count the sign bit.


This does *not* give you the amount of value representation bits (exclusive
of the sign bit). See my explanation elsethread.
 
J

Jack Klein

Simon Biber posted:



The quantity of object representation bits, yes.




Incorrect. Except for unsigned char, all integer types may contain padding
bits. For info on how to find the quantity of value representation bits, try
this:

http://groups.google.ie/group/comp.lang.c/msg/1902a6f344859148?hl=en&

Have you ever programmed on, or even seen, a system that did actually
have padding bits in the integer types?

On top of that, even if this code will be used on such an
implementation, why do you think it matters? The purpose of the code
is to calculate an array of characters guaranteed to be able to hold
the text string representation in decimal of any value that can be
held in a signed int, including a possible leading '-' and the
terminating '\0'.

If indeed this code is ever run on a platform with padding bits in
signed int, an extremely improbably possibility, then perhaps this
buffer will be several bytes longer than absolutely necessary. So
what?
 
F

Frederick Gotham

Jack Klein posted:
Have you ever programmed on, or even seen, a system that did actually
have padding bits in the integer types?


Irrelevant. This is comp.lang.c, not comp.lang.c.my.implementation.

If you wish to discuss the perceived irrationality in the Standard's non-
restriction of integer types to contain padding bits, then comp.std.c would
be a better-suited venue.

On top of that, even if this code will be used on such an
implementation, why do you think it matters?


I was merely correcting a false assumption. The following does not yield the
amount of value representation bits in an integer type:

CHAR_BIT * sizeof(IntType)
 
B

Barry Schwarz

Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

char * itoa(int a)
{
#define BUFFSIZE 50

static char str[BUFFSIZE];

int i = sizeof(str) - 1;

int neg = 0;
if (a < 0) {neg = 1; a = -a;}
do
{
str[--i] = '0' + a % 10;
}
while ((a = a / 10) && i >= 0);

if (neg) str[--i] = '-';
return &str;
}

Couple of notes on this.

I didn't want to have to pass in a char * to a buffer, so I return a
pointer to a char in the static buffer - I think 50, although chosen
arbitrarily, should be big enough. I did think my making this exact
for the implementation, and toyed with the idea of somehow using
MAX_INT in limits.h to do this. However, I couldn't figure that out,
e.g., I thought of stringifying MAX_INT and then using sizeof on it,
but I don't think I can do that as a pre-processor thing and I can't
think of any other way to do this.

Related. Where I #define BUFFSIZE, I'm not sure that this is good - for
example, if this source is included in a project in which there's
already a #define BUFFSIZE something, will my #define cause a problem,
if so, how can I avoid that, e.g., just use a const instead?

I also didn't/don't feel I can implement a 'radix' parameter - so I
left that out.

Thanks,

gareth



Remove del for email
 
E

ena8t8si

Hi.

Just as a learning exercise, I've come up with an implementation of
itoa() without using something like sprintf. However, I can't figure
out how to extend it to deal with negative numbers - any help
appreciated!

A standard way of dealing with integer conversion
so negative numbers work is like so:

negative = 0;
if(a<0) negative = 1;
else a = -a;

... now print digits for a, a always <= 0 ...

if(negative) add the leading '-'

The reason for always using negatives is to avoid
problems dealing with a most negative number, which
often can't be made positive.

For printing the digits of a negative number, the
technique of dividing the number by 10 and using
the remainder to calculate the next digit still
works, except it has to be adapted because the
remainders can be negative. Probably you can
figure out how to do that if you play around with
it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top