1
#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?

3
  • Hi Althaf. Can you give us the error you are getting and where it happens? That will help anyone trying to answer this question. Commented Jul 2, 2020 at 8:21
  • And turn on warnings of your compiler because it would have given you warnings about the return of bin and of the assignment in main. Commented Jul 2, 2020 at 8:56
  • Note: with res=bin(num); you think you copy the string returned by bin. But that doesn't work in C. In C you have to use strcpy() Commented Jul 2, 2020 at 8:57

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.