954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Big Integer Problems

Hello.
I've been doing a small application that will go (using only unsigned integer values) from 0 to 4026531840 and print them out.
My code is next:

#include <stdio.h>
#include <inttypes.h>
int main(void){
uint64_t cnt;
for(cnt=0;cnt<=4026531840;cnt++){
printf("%d\n",cnt);
}
}

The only problem that i have is the last numbers are negative (the really last one is -268435456).
I also tried using:

#include <stdio.h>
int main(void){
unsigned long long int cnt;
for(cnt=0;cnt<=4026531840;cnt++){
printf("%d\n",cnt);
}
}

but the problem persists.
I'm using gcc:
Target: x86_64-linux-gnu
gcc version 4.6.1 (Debian 4.6.1-4)
on my linux machine:
Linux mysql 3.0.0-1-amd64 #1 SMP Sat Aug 27 16:21:11 UTC 2011 x86_64 GNU/Linux
Any ideas?

jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
 

You're printing the value with %d, which is limited to signed int. The C99 specifier for unsigned long long is %llu:

#include <stdio.h>

int main(void)
{
    unsigned long long int cnt;
    
    for (cnt = 0; cnt <= 4026531840; cnt++)
        printf("%llu\n", cnt);
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks. Problem solved.

jen140
Junior Poster
117 posts since Jan 2009
Reputation Points: 11
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: