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?

Edited 5 Years Ago by jen140: n/a

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);
}
Comments
Demonstrates an amazing ability to quickly solve problems not easily solved by others. Shows great depth of knowledge and a solid background based on in depth studies.
This question has already been answered. Start a new discussion instead.