0

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?

h101
  • 31
  • 1
  • 5
  • 3
    What is the desired output? `"%lX"` is not the correct specifier for `uint64_t`. – mch Nov 17 '16 at 15:04
  • the right printf format for `uint64_t` is [`PRIu64` or `PRIx64`](http://stackoverflow.com/q/9225567/995714), not `%lX` – phuclv Nov 17 '16 at 15:07
  • @mch I have tried using printf("%" PRId64 "\n", set1), but that isnt currently working either. It keeps on giving me errors so Im not sure what to use to print out the numeral solution – h101 Nov 17 '16 at 15:07
  • because you haven't included `inttypes.h` – phuclv Nov 17 '16 at 15:10

1 Answers1

2

Assuming your platform has long size 64 bits, the correct format for your printfs 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;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 2
    `%lu` definitely is not for `uint64_t` because long is not necessarily 64 bits – phuclv Nov 17 '16 at 15:10
  • @LưuVĩnhPhúc Well, I added the "cross solution" – LPs Nov 17 '16 at 15:11
  • 1
    Your first snippet is incorrect and the second one is almost, so why not delete the first one? The OP wants hex so you should change to `PRIx64`. – Lundin Nov 17 '16 at 15:17
  • @Lundin Where is stated that the output must be HEX? I understood the **problem is** that the output is in HEX. Otherwise OP code can be ok... – LPs Nov 17 '16 at 15:21
  • @LPs It should be obvious from looking at their code and output. – Lundin Nov 18 '16 at 07:14