Write a C program to convert an integer into a binary
string?



Write a C program to convert an integer into a binary string?..

Answer / vadivelt

#include<stdio.h>
char *IntToBinString(int no);
main()
{
int no;
printf("ENTER THE NO: ");
scanf("%d",&no);
printf("\nBINARY O/P STRING:\n%s",IntToBinString(no));
getch();
}
char *IntToBinString(int no)
{
char *ptr;
int i, size;
size = sizeof(int)*8;
ptr = (char *)malloc(sizeof(int)*8);
for(i = size - 1; i >= 0; i--)
{
if(no >> i & 0x01)
{
*ptr++ = 49;
}
else
{
*ptr++ = 48;
}
}
*ptr = '\0';
return (ptr - size);
}

Is This Answer Correct ?    9 Yes 3 No

Post New Answer

More C Interview Questions

what is different between auto and local static? why should we use local static?

1 Answers  


How can a program be made to print the line number where an error occurs?

1 Answers  


What does it mean when the linker says that _end is undefined?

1 Answers  


I want tcs placement papers of 2004-2009 , its urgent

6 Answers   TCS, Wipro,


What is the output of the following program #include<stdio.h> main() { int i=0; fork(); printf("%d",i++); fork(); printf("%d",i++); fork(); wait(); }

8 Answers   ADITI, Adobe,


What is a class?

3 Answers  


What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25

1 Answers  


Explain how are 16- and 32-bit numbers stored?

1 Answers  


Is null valid for pointers to functions?

1 Answers  


a c variable cannot start with a) an alphabet b) a number c) a special symbol d) both b and c above

1 Answers  


Write a program with dynamically allocation of variable.

1 Answers   Atos, Atos Origin,


what are bit fields in c?

3 Answers  


Categories