Althaf
Althaf

Reputation: 29

Decimal to binary using string in c....stuck with some conceptual error

#include<stdio.h>

char bin(int);

int main()
{
    setbuf(stdout,NULL);
    int num;
    char res[50];
    printf("Enter the number: ");
    scanf ("%d",&num);
    res=bin(num);
    printf("%s",res);
    return 0;
}

char bin(int num)
{
    char str[50];
    int i,val;
    for(i=0;num>=0;i++)
    {
        val=num%2;
        str[i]=val;
        num=num/2;
    }
    return str;
}

I really cant understand the error in the usage of strings... to convert the decimal to binary. Whats the conceptual error Im not following?

Upvotes: 1

Views: 105

Answers (2)

anastaciu
anastaciu

Reputation: 23802

Returning str amounts to returning a local variable, you can't do it, what you can do is to return a pointer to a previously allocated memory block that works as an array (as an alternative to the oher answer, which is a good solution).

To do this you can declare str as a pointer, allocate memory for it and return it, making sure the variable to which the value is assigned is also a pointer, all the rest can remain the same.


There are, however, problems with the bin function.

  • Consider the statement:
   str[i] = val;

This will not work as expected you are assigning the int result of the operation, which will be 1 or 0, you need to convert this value to the respective character.

  • The loop for (i = 0; num >= 0; i++) is an infinite loop because num will never be negative, unless you provide it a negative number in which case it will break in the first iteration, that is to say this code only works with positive integers. You need > instead of >=.

  • Finally you need to null terminate the string when the conversion is complete.

Corrected code (Online):

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

char *bin(int); //return pointer

int main() {
    setbuf(stdout, NULL);
    int num;
    char *res; //use pointer to receive string assignment
    printf("Enter the number: ");
    scanf("%d", &num);
    res = bin(num);
    printf("%s", res);
    return 0;
}

char *bin(int num) {
    char *str = malloc(50); // allocate memory
    int i, val;

    for (i = 0; num > 0; i++) { // replacing >= with >
        val = num % 2;
        str[i] = val + '0'; // convert to character
        num = num / 2;
    }
    str[i] = '\0'; //null terminate the string
    return str;
}

Note that you should also check for the inputed value, if it is larger than what an int variable can hold it will result in undefined behavior.

Upvotes: 1

Stephan Lechner
Stephan Lechner

Reputation: 35154

char is a single character, so char bin(int) will not be able to return a string (i.e. a null-terminated array of characters). And you cannot "return" an an array of characters, because C does not allow to return any array as function result. You can just pass/return pointers to the begin of such arrays.

So I'd suggest to change the interface of bin to reicieve the result buffer as parameter. Don't forget to "close" the string, i.e. to write the string termination character after the last "actual" character:

void bin(int num, char* resultBuffer) { 
   ...
   resultBuffer[i] = '\0';
}

In main, you call it then like

bin(num, res);

Upvotes: 3

Related Questions