so i have to make a function that displays the binary representation of a 32 bit integer

this is what i have

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

#include<stdio.h>

void inttobin(int c)
{

         char * s ;    
    char * p ;
  size_t n = 8*sizeof(unsigned int);
      p  = malloc(n*sizeof(char));


  while (n-- > 0){



   s  =  ((c >> n) & 1) ? "1" : "0";

    strcat(p,s) ;

  }

  printf("%s",p) ;
}

int main()
{


inttobin(0x1234567) ;

}

it prints out the right answer but with some garbage values at the first 3 characters of the string and I dont know how to fix it can you guys help me?

Edited 5 Years Ago by rockerjhr: n/a

after allocating the array you have to initialize it to 0 so that strcat() will work correctly the first time. You also have to null-terminate the array after the loop is finished so that printf() will work correctly. Both issues can be solved at the same time by calling memset() to flood the array with '\0' bytes.

p  = malloc(n+1); // make room for null-terminating character
 memset(p,0,n+1); // initialize the array

Edited 5 Years Ago by Ancient Dragon: n/a

Both issues can be solved at the same time by calling memset() to flood the array with '\0' bytes.

p  = malloc(n+1); // make room for null-terminating character
 memset(p,0,n+1); // initialize the array

or a simple loop for (i=0; i<n+1; i++) p[i] = 0; which is most likely something you already learned.

Both issues can be solved at the same time by calling memset() to flood the array with '\0' bytes.

or a simple loop for (i=0; i<n+1; i++) p = 0;

All that's necessary is to set the first character to '\0' to make it a valid string for strcat():

p = malloc(n);
p[0] = '\0';

strcat() will then ensure that the result is properly terminated for printf().

All that's necessary is to set the first character to '\0' to make it a valid string for strcat():

p = malloc(n);
p[0] = '\0';

strcat() will then ensure that the result is properly terminated for printf().

The string still must be null-terminated, which is why I suggested flooding the entire buffer with '\0' before doing anything. And you need to add 1 to n in the malloc line.

The string still must be null-terminated, which is why I suggested flooding the entire buffer with '\0' before doing anything.

The string is null terminated. strcat() properly appends a null character to the destination, so the only issue is making sure that the string is null terminated before the first call to strcat().

And you need to add 1 to n in the malloc line.

Yes indeed.

The string is null terminated. strcat() properly appends a null character to the destination, so the only issue is making sure that the string is null terminated before the first call to strcat().

I knew that -- I was just testing you to see if you did too :) :)

I was just testing you to see if you did too

Silly dragon, how many times have you seen me not know something about C? ;)

This article has been dead for over six months. Start a new discussion instead.