我正在尝试创建一个程序,只是将给定数字的数字除以并打印出剩余部分和给定数字的解决方案除以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("%lX\n",set1);
uint64_t remainder = set1%10;
printf("%lX\n",remainder);
set1= set1/10;
printf("%lX\n",set1);
}
目前,这个输出给了我以下
36C06
2
579A
我如何才能正确输出分割值和余数?
答案 0 :(得分:2)
假设您的平台long
尺寸为64 bits
,则printf
的正确格式为%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;
}
使用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;
}