LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

binary//keith

 // code not working, as soon as i added my main it gave me errors, can someone fix it

#include <ansi_c.h>
#include <string.h>

char PrintBin(int,char);

int main()
{
   
    PrintBin (int n,*string);
    return 0;
   
}
char *PrintBin (int n, char *string)
{
 int  len = 0;
 
 if (n < 0) return NULL; // Number must be non-negative
 
 // Recursively call the function for 1 to n-1 bits
 if (n > 1) string = PrintBin (n / 2, string);
 
 // Dynamic allocation of memory
 if (!string) {
  string = malloc (2);
  memset (string, 0, 2);
 }
 else {
  len = strlen (string);
  string = realloc (string, len + 2);
  string[len] = 0;
 }
 
 // Print the last bit
 sprintf (string + strlen (string), "%d", n % 2);
 
 return string;
}

0 Kudos
Message 1 of 2
(3,024 Views)
Duplicated post: answered here


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 2
(3,014 Views)