|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
overflow on long variable type
Hey,
I just after some help with a little bug that is annoying me. I am just starting with C (have done other languages) so excuse any blatant stupidity. I am using a long variable type and simply trying to store the value 100000 in it (as part of a larger program of course). I looked up the limits.h file to check the max value for both int and long and saw that the max value for long was approx 2e09. But despite this if i try to allocate anything over about 32000 (which is the upper limit for int), say 34000 it overflows and comes up with a negative value like -31560 or something to that effect. Some code to demonstrate the problem: #include <stdio.h>; #include <math.h>; void main() { long i; for (i = 0; i <=100000; i += 2000) { printf("%d ", i); } } Output: 0 2000 4000 6000 8000 10000 12000 14000 16000 18000 20000 22000 24000 26000 28000 30000 32000 -31536 -29536 -27536 -25536 -23536 -21536 -19536 -17536 -15536 -13536 -11536 -9536 -7536 -5536 -3536 -1536 464 2464 4464 6464 8464 10464 12464 14464 16464 18464 20464 22464 24464 26464 28464 30464 32464 -31072 Any help wold be greatly appreciated, as it is starting to piss me off Cheers Fozz |
|
#2
|
|||
|
|||
|
Can you try chaning your code to read
int main() { long i; for (i = 0; i <= 100000; 1 += 2000) { printf("%d\n", i); } return 0; } and tell us what you get? By the way, what version C compiler are you using? |
|
#3
|
||||
|
||||
|
I don't have the time nor inclination to look up the proper terminology at this very moment, but you have to add a type qualifier [incorrect term, I'm sure] to a numeric literal if it is not the default.
The integer type default is int and the floating point default is double. int only ranges over +/- 32K. To make a literal a long int, you need to append an 'L' to it. To make it unsigned, you need to append a 'U'. To make a double a float, append an 'F'. The 'L' and the 'U' and the 'F' can also be in lower case. So your code should read: Code:
#include <stdio.h>;
#include <math.h>;
void main()
{
long i;
/* make the test value a long
also make the smaller values long in order to eliminate
the need for automatic type conversion. Not absolutely
necessary, but a good practice.
*/
for (i = 0L; i <=100000L; i += 2000L)
{
/* the format specifier also needs to be long int or else you
will still get an erroneous output
*/
printf("%ld ", i); /* "ell dee" */
}
}
Having the wrong format specifier in a printf can cause an apparent error. printf will take whatever value you give it and interpret it as what the format specifier says. In one example, we had a long int output and I used an 'L' instead of an 'l' in order to make it more readable (ie, that it not be confused with a one). It totally wacked out our output, which was a strictly formatted ASCII string requiring specific fields to start at specific places in the string. That's when I learned that an 'L' format specifier means long double; a long int must be indicated by a lower-case 'l'. All that having been said, your original error may have been directly caused by the literal not having a type qualifier or, more likely, by your having the wrong format specifier in your printf. Either way, both corrections should be made, not just the latter. Last edited by dwise1_aol : July 3rd, 2003 at 09:47 AM. |
|
#4
|
|||
|
|||
|
Thanks for the help.
I am using an old Borland Turbo C compiler v2.01 (i think). I changed the code to the following: #include <stdio.h>; #include <math.h>; void main() { long i; for (i = 0; i <=100000; i += 2000) { printf("%ld ", i); /* changed the %d to %ld /* } } Gave following output: 0 2000 4000 6000 8000 10000 12000 14000 16000 18000 20000 22000 24000 26000 28000 30000 32000 34000 36000 38000 40000 42000 44000 46000 48000 50000 52000 54000 56000 58000 60000 62000 64000 66000 68000 70000 72000 74000 76000 78000 80000 82000 84000 86000 88000 90000 92000 94000 96000 98000 100000 So problem solved. I had an incling that it was something to do with the place holder (because the loop was terminating but the output was wrong) in the printf statement but did not know exactly what was wrong or what the place holder for a long int was. Also, can anyone recommend any free c compilers that are a little more recent than the one i am using currently. Thanks again Fozz |
|
#5
|
||||
|
||||
|
Quote:
Take a look on my sockets programming resources page at http://members.aol.com/DSC30574/sockets/resources.html . On there, I mention, briefly discuss, and provide links to some free compilers, including Borland's v5.5 and Dev-C++ which uses the MinGW port of gcc. I don't know why I forgot to include LCC, except perhaps because LCC is C only while the other two also give you C++. Personally, I use MinGW's gcc by itself on the command line, but that's because I'm training for Linux as well. You might want to post this question as a new thread so that you can get more input. Last edited by dwise1_aol : July 4th, 2003 at 11:01 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > overflow on long variable type |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|