C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 3rd, 2003, 06:57 AM
Fozz Fozz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Northern NSW
Posts: 2 Fozz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old July 3rd, 2003, 09:06 AM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 44 m 30 sec
Reputation Power: 8
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?

Reply With Quote
  #3  
Old July 3rd, 2003, 09:45 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,868 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 3 h 14 m 44 sec
Reputation Power: 480
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.

Reply With Quote
  #4  
Old July 3rd, 2003, 06:08 PM
Fozz Fozz is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Northern NSW
Posts: 2 Fozz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #5  
Old July 3rd, 2003, 07:13 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,868 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 3 h 14 m 44 sec
Reputation Power: 480
Quote:
Originally posted by Fozz

Also, can anyone recommend any free c compilers that are a little more recent than the one i am using currently.

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > overflow on long variable type


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT