0

This code is for converting a decimal number in its equivalent in any base numeric system.

The reversed result is stored in temp.

#include <stdio.h>
#include <math.h>
int main(){
    long decimal;
    int b = 0,i = 0,n,j = 0;
    printf("Enter Decimal Number :");
    scanf("%ld",&decimal);
    char temp[decimal],base[decimal];
    printf("Enter Base :");
    scanf("%d",&b);
    for(;decimal != 0;decimal /= b){    
      n = decimal % b;
      if(n >= 0 && n <= 9)
      temp[i++] = n + 48;
      else if(n >= 10)
      temp[i++] = n + 55;
    }
    for(j = 0;i >= 0;) //Reversing temp and storing it in base
       base[j++] = temp[i--];
       printf("Base Equivalent : %s",temp);
       printf("Base Equivalent : %s",base);
    return 0;
}

The temp part need to be reversed to get the correct result, yet, temp does not get reversed and stored in base. What's wrong with my code?

3 Answers3

1
char temp[decimal],base[decimal];

wow. What if decimal is negative? Do you really need to allocate 4GB memory to convert 2147483647? Thank god you do not read long long int Then it would require 2*9 223372036854775807 bytes (many hundreds of petabytes) of memory

Use functions for similar tasks:

char *reverse(char *str)
{
    char *wrk = str, *end = str;

    if(str && *str)
    {
        while(*(end + 1)) end++;
        while(end > wrk)
        {
            char temp = *end;
            *end-- = *wrk;
            *wrk++ = temp;
        }
    }
    return str;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
1

The main problem is in the line base[j++] = temp[i--];. Note that the i-- expression returns the value of i before the decrement (and then decrements i). You need the predecrement operation, instead:

for (j = 0; i > 0;) // Note the change to i > 0
    base[j++] = temp[--i];

There is also a potential issue with the lengths of your character arrays. Why do you assume the value of the input number (decimal) will be appropriate for the length of those string?1 Give them fixed lengths, instead – one that will be large enough to handle all cases. You also don't add the required nul terminators to your strings; this can be achieved by initialising them with 'empty' strings (will which set all elements to zero):

char temp[64] = "", base[64] = "";

1 For example, for an input of 2 and a base of 2, the string required for the binary representation (10) will need to be 3 characters long (one for the terminator).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

strings in c must be null-terminated.
You just need to add '\0' to the end of both of your array to make it work.

e.g.:

#include <stdio.h>
#include <math.h>
int main(){
    long decimal;
    int b = 0,i = 0,n,j = 0;
    printf("Enter Decimal Number :");
    scanf("%ld",&decimal);
    char temp[64],base[64];
    printf("Enter Base :");
    scanf("%d",&b);
    printf("MM: %ld, %d", decimal, b);
    for(;decimal != 0;decimal /= b){    
      n = decimal % b;
      if(n >= 0 && n <= 9)
      temp[i++] = n + 48;
      else if(n >= 10)
      temp[i++] = n + 55;
    }

    // add null terminators
    temp[i] = '\0';
    base[i] = '\0';
    i--;

    for(j = 0;i >= 0;) //Reversing temp and storing it in base
       base[j++] = temp[i--];
       printf("Base Equivalent : %s",temp);
       printf("Base Equivalent : %s",base);
    return 0;
}

godbolt example

Turtlefight
  • 9,420
  • 2
  • 23
  • 40