unsigned int nBolum = 0;
unsigned long nKalan = 0L;
unsigned long ulA = 0L;
unsigned long ulB = 0L;
...
        printf("ulA=%u\n",ulA);
        printf("ulB=%u\n",ulB);
        nBolum = (unsigned int) ulA/ulB;
        nKalan = (unsigned long) (ulA%ulB);
        printf("nBolum = %u\n",nBolum);
        printf("nKalan = %u\n",nKalan);
        ...

it prints;

ulA=2607503366
ulB=16777215
nBolum = 155
nKalan = 7100321

to the screen.

But the nKalan value(the remaining value from the division) must be 7035041. why is it calculating it wrong?
what is the problem?

thanks

> what is the problem?
No idea, but first some warnings.

gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c: In function `main':
foo.c:10: warning: unsigned int format, long unsigned int arg (arg 2)
foo.c:11: warning: unsigned int format, long unsigned int arg (arg 2)
foo.c:15: warning: unsigned int format, long unsigned int arg (arg 2)

That fixed, it seems fine to me

#include<stdio.h>

int main(void)
{
    unsigned int nBolum = 0;
    unsigned long nKalan = 0L;
    unsigned long ulA = 2607503366UL;
    unsigned long ulB = 16777215UL;

    printf("ulA=%lu\n", ulA);
    printf("ulB=%lu\n", ulB);
    nBolum = (unsigned int) ulA / ulB;
    nKalan = (unsigned long) (ulA % ulB);
    printf("nBolum = %u\n", nBolum);
    printf("nKalan = %lu\n", nKalan);

    return 0;
}

$ gcc -W -Wall -ansi -pedantic -O2 foo.c
$ ./a.exe
ulA=2607503366
ulB=16777215
nBolum = 155
nKalan = 7035041
This article has been dead for over six months. Start a new discussion instead.