[comp.lang.c] Big numbers in C?

robertl@killer.UUCP (Robert Lord) (07/26/87)

I was wondering....How do I use large numbers (over 32000) in C?  I know about
float type, but that give it in wierd numbers.  I need numbers in the hundreds
of millions, and I need them in real format. (i.e. 100000000), not float
format.  If there a little script that could change float to real?

Also, I am having a little trouble with this statement:

strtol(data.number);

Where:  data.number[1] == 5
        data.number[2] == 7  /*Or whatever...Just examples*/
        data.number[3] == \0

Is there a way to convert this string to an numerical value?  The reason I
have to have it like this is because I need to only allow 8 characters of
input.

           Thanks in advance,
                          Robert Lord
                            ..!ihnp4!killer!robertl

ark@alice.UUCP (07/28/87)

In article <1192@killer.UUCP>, robertl@killer.UUCP writes:
> I was wondering....How do I use large numbers (over 32000) in C?  I know about
> float type, but that give it in wierd numbers.  I need numbers in the hundreds
> of millions, and I need them in real format. (i.e. 100000000), not float
> format.  If there a little script that could change float to real?

On most C implementations, the "long" type is stored as a 32-bit
signed integer.  That means you can store integer values between
-2147483648 and 2147483647, inclusive.  Is that big enough?

A quick example:

	long foo;
	foo = 123456789L;
	printf ("%ld\n", foo);

Notice the L at the end of the constant to say that it is a long
number, and the %ld format item to print it.

> Also, I am having a little trouble with this statement:
> 
> strtol(data.number);
> 
> Where:  data.number[1] == 5
>         data.number[2] == 7  /*Or whatever...Just examples*/
>         data.number[3] == \0

When using something like strtol, the elements of the array you
are converting should be representations of characters, not
small integers.  The following should work:

	data.number[0] = '5';
	data.number[1] = '7';
	data.number[2] = 0;		/* or '\0' but not '0' */

Also note that subscripts in C start from 0, not 1.

Alan_Cote.DlosLV-Comm@Xerox.COM (07/28/87)

Robert Lord <robertl@killer.uucp> writes:

>I was wondering....How do I use large numbers (over 32000) in C?  I
know about
>float type, but that give it in wierd numbers.  I need numbers in the
hundreds
of millions, and I need them in real format. (i.e. 100000000), not float
>format.  If there a little script that could change float to real?

If, by "real", you mean "integer", then try the "long" type.  That will
give you a 32-bit, signed number.

>Also, I am having a little trouble with this statement:
>
>strtol(data.number);
>
>Where:  data.number[1] == 5
>        data.number[2] == 7  /*Or whatever...Just examples*/
>        data.number[3] == \0
>
>Is there a way to convert this string to an numerical value?  The
reason I
>have to have it like this is because I need to only allow 8 characters
of
>input.
>
>           Thanks in advance,
>                          Robert Lord
>                            ..!ihnp4!killer!robertl

If this read a little differently, for example:

        data.number[1] == '5'
        data.number[2] == '7'  /*Or whatever...Just examples*/
        data.number[3] == '\0'

then strtol(data.number) should return a long value (assuming, of
course, that you have declared strtol() to return (long) :=).  If that
doesn't work, try using sscanf().

	- Al Cote'

	"Mine is the last voice you will ever hear... Don't be alarmed"
		- F. Sinatra

ayac071@ut-ngp.UUCP (William T. Douglass) (07/29/87)

In article <8529@brl-adm.ARPA> Alan_Cote.DlosLV-Comm@Xerox.COM writes:
>Robert Lord <robertl@killer.uucp> writes:
>>Also, I am having a little trouble with this statement:
>>strtol(data.number);
>>
>>Where:  data.number[1] == 5
>>        data.number[2] == 7  /*Or whatever...Just examples*/
>>        data.number[3] == \0
>
>If this read a little differently, for example:
>        data.number[1] == '5'
>        data.number[2] == '7'  /*Or whatever...Just examples*/
>        data.number[3] == '\0'
>
>then strtol(data.number) should return a long value (assuming, of


Just wondering, but shouldn't that be indexed from 0, from 1,
in order for strtol to work, so that

         data.number[0] == '5'
         data.number[1] == '7'
         data.number[2] == '\0'

or am I really hopelessly confused?  


Bill Douglass
ayac071@ngp.UUCP

(This looks like me...)

hild@infbs.UUCP (07/30/87)

Don't forget the character with index 0. There might be an '\0' in it.
Is this what you wanted to say, Al ?

... Frank ...