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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

When reallocating memory if any other pointers point into the same piece of memory do you have to readjust these other pointers or do they get readjusted automatically?

806


How many bytes are occupied by near, far and huge pointers (dos)?

668


while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above

738


Can we compile a program without main() function?

634


Is swift based on c?

638






What are valid operations on pointers?

668


What is %g in c?

618


Explain what is the difference between #include and #include 'file' ?

585


Why can't I perform arithmetic on a void* pointer?

635


What is c language used for?

559


What are the two forms of #include directive?

644


By using C language input a date into it and if it is right?

574


Explain the ternary tree?

601


A variable that is defined in a specified portion of a program but can be used throughout the program a) global variable b) local variable c) character d) none

729


What is structure pointer in c?

571