简体   繁体   中英

How to use arithmatic operations on uint64_t?

I am trying to make a program that simply divides the number given number and prints out the remainder and the solution of the given number divided by 10. However my code isnt printing out the correct values. Here is the following code:

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
uint64_t divide(uint64_t,uint64_t);

int main(int argc, char *argv[])
{

        uint64_t num1 = 224262;
        uint64_t num2 = 244212;
        divide(num1,num2);

}


uint64_t divide( uint64_t set1, uint64_t set2 )
{

        printf("%lX\n",set1);
        uint64_t remainder = set1%10;
        printf("%lX\n",remainder);
        set1= set1/10;
        printf("%lX\n",set1);
}

Currently the output of this gives me the following

36C06
2
579A

How would I have it so that it correctly outputs the divided value and the remainder?

Assuming your platform has long size 64 bits , the correct format for your printf s is %lu

uint64_t divide( uint64_t set1, uint64_t set2 )
{
    printf("%lu\n",set1);
    uint64_t remainder = set1%10;
    printf("%lu\n",remainder);
    set1= set1/10;
    printf("%lu\n",set1);

    return set1;
}

Using predefined format specifier of inttypes.h you can write

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>

uint64_t divide(uint64_t,uint64_t);

int main(int argc, char *argv[])
{
    uint64_t num1 = 224262;
    uint64_t num2 = 244212;

    divide(num1,num2);
}


uint64_t divide( uint64_t set1, uint64_t set2 )
{
    printf("%"PRIu64"\n",set1);
    uint64_t remainder = set1%10;
    printf("Reminder = %"PRIu64"\n",remainder);
    set1= set1/10;
    printf("Division = %"PRIu64"\n",set1);

    return set1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM