简体   繁体   中英

Trouble with a decimal to hexadecimal function that doesn't return anything

When I compile this nothing returns and it is just empty space where I can type, but nothing happens instead of computing the program, Any reasons why?

#include <stdio.h>
void decimalToHex(long int decValue);

int main()
{
    long int decValue = 20;
    decimalToHex(decValue);

    return 0;
}

void decimalToHex(long int decValue)
{
    long int remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];
    quotient = decValue;
    while(quotient!=0)
    {
        temp = quotient % 16;
        if( temp < 10)
        temp =temp + 48;
        else
        {
            temp = temp + 55;
            hexadecimalNumber[i++]= temp;
            quotient = quotient / 16;
        }
    }
    printf("Equivalent hexadecimal value of decimal number %ld: ",decValue);
    for(j = i -1 ; j> 0; j--)
    printf("%c",hexadecimalNumber[j]);
    printf("\nGoodbye!\n");
}
            hexadecimalNumber[i++]= temp;
            quotient = quotient / 16;

Hmm shouldn't this be outside the else? Yes it should.

while(quotient!=0)

While we're at it, this should be a do/while loop not a while loop. Always go around the loop once.

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