C语言 如何在uint64_t上使用算术运算



我正在尝试制作一个程序,简单地将给定的数字除以给定的数字,并打印出给定数字除以10的余数和解。然而,我的代码没有打印出正确的值。下面是代码:

#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("%lXn",set1);
        uint64_t remainder = set1%10;
        printf("%lXn",remainder);
        set1= set1/10;
        printf("%lXn",set1);
}

当前的输出结果如下

36C06
2
579A

我该如何让它正确地输出除数和余数呢?

假设您的平台具有long大小64 bits,则printf s的正确格式为%lu

uint64_t divide( uint64_t set1, uint64_t set2 )
{
    printf("%lun",set1);
    uint64_t remainder = set1%10;
    printf("%lun",remainder);
    set1= set1/10;
    printf("%lun",set1);
    return set1;
}

使用inttypes.h的预定义格式说明符可以写

#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;
}

相关内容

  • 没有找到相关文章

最新更新