Question

I wrote this function to reverse a number. I will have test cases that are up to 2^32. I need the function to return unsigned ints. My question is this: why wont this print?

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

unsigned int reverse(unsigned int a);

int main(){

    printf("%u\n",reverse(987654321));
    //printf("%ui\n",reverse(987654321)); //this wont work either

    return 0;
}

unsigned int reverse(unsigned int a)
{
    int temp=0;
    while(a>0)
    {
        temp=10*temp+a%10;
        a/=10;
    }
    return temp;
}

Yes I did forget about the proto-type... Bear with me I have been doing Java and Lisp lately. However, my compiler keeps giving me a warning saying I have extra characters in the format string. It also does this if I have type "long long int" and I use "%lli% in the format string, which I also tried.

Was it helpful?

Solution

You forgot to include prototype for your function before main.

unsigned int reverse(unsigned int a);

OTHER TIPS

The number probably is just too big for an unsigned on your architecture.

Remember to always enable maximum warning level for your compiler. This should have told you that reverse is not known where you use it, that you are passing an int to the %u format and also that your number ist too big.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top