Originally Posted by
EuroDominus
int_to_binary(x) returns the address of the first element of array b, the array's address is a long integer so you can't use it in printf with "%s".
Try: printf("%s\n", *(int_to_binary(x))
I tried putting in that printf state but my output is still (null)
Code:
#include <stdio.h>
#include <stdlib.h>
char *int_to_binary(int);
char b[80];
int main(void)
{ // Call function int_to_binary to print a number in binary
int x=0;
char *ptr;
if(setvbuf(stdout, NULL, _IONBF,0))
{
perror("failed to change the buffer of stdout");
return EXIT_FAILURE; //clears buffer
}
printf("This program will convert the users input from decimal to binary.\n");
printf("Enter a decimal number: ");
scanf("%d",&x);
ptr = int_to_binary(x);
printf("%s\n", *(int_to_binary(x))); //when using any of these 2 printf statements and i run the prgram the output is (null) after inputting my decimal
//printf("%s\n", int_to_binary(x));
return 0;
}
char *int_to_binary(int x)
{
int i;
for(i=31; i>=0; i--)
{
b[i]= x%2;
x/=2;
}
b[33]='\0';
//for(i=0; i<=31;i++) //these are the two lines that I have added in order to get the correct output, but according to the project i shouldnt need them correct?
//printf("%d",b[i]);
return b;
}