Quote:
Originally Posted by H_TeXMeX_H
Use 'unsigned int' instead of 'int'. You may need to use 'unsigned long int' if numbers get really large.
|
On a 32 bit x86 architecture, you would need 'unsigned long long'.
On a 32 bit x86 architecture, 'unsigned long int' is still the same size as 'unsigned int'
Quote:
Originally Posted by noir911
convert gigabyte into byte.
|
You should be aware that "gigabyte" is often used to mean 10 to the ninth power, rather than the meaning you seem to intend: 2 to the thirtieth power. Many people observe the convention that GiB unambiguously means 2 to the thirtieth power bytes.
Quote:
It will take 50% of the value given and then convert that value into byte.
|
Was it luck or understanding that made you multiply by .5 rather than divide by 2?
Dividing by 2 would leave an integer result, so 23/2 would be 11 just like 22/2. Also an integer result would have just 32 bits when multiplied by 1024*1024*1024, so even if that result were stored in an unsigned long long, it would have already been truncated.
Multiplying an int by .5 creates a temporary of type double, which can be multiplied by 1024 three times with no truncation. Then all the truncation occurred when you stored that double into a 32 bit integer. That truncation would be avoided if you had stored into a long long.
Quote:
printf ("Byte size is: %d\n", byte);
|
But then printing a long long via printf is tricky. In fact I don't remember how you can do that.
You could use some ugly kludge such as
Code:
unsigned long long byte;
...
printf("Byte size is: %u%09u\n",
(unsigned)( byte/1000000000 ),
(unsigned)( byte%1000000000 ) );
Edit: GCC (at least version 3.4.6 that I tried) seems to support %lld for printing long long. Previous C compilers I have used didn't. So all you needed was:
Code:
unsigned long long byte;
...
printf("Byte size is: %lld\n", byte);