I am getting garbage value. Is it need to free 's' every time or the reason is something else.
please,help.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
char* toBin(int);
void rec(int);
void main()
{
int n;
char *out;
clrscr();
printf("\n Enter no : ");
scanf("%d",&n);
out=toBin(n);
printf("\n %s",out);
free(out);
// rec(5);
getch();
}
/*void rec(int n)
{
if(n<=0)
{
return;
}
rec(n-1);
printf("\n %d",n);
}*/
char* toBin(int d)
{
int d1=d,r=0,i=0;
char *s="";
// free(s);
// s=(char*)malloc(10*sizeof(char));
while(d1>0)
{
r = d1 % 2;
d1 = d1 / 2;
s[i] = r + 48;
i++;
}
flushall();
strrev(s);
flushall();
printf("\n rev : %s",s);
return s;
}
/*output
Enter No : 33
% : 100001
s% : 33
*/