tags:

views:

165

answers:

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

// Print out the binary value of an integer

void binval (int num);
int get_number();

main () {
  int num;

  num = get_number();
  printf("Num = %d\n",num);
  binval(num);

}

void binval (int num) {
  int val = 0;
  int test;

  if (!num) {
    printf("\n");
    return;
  }


  test = num & 0x0001;
  if (test == 1) {
    val = 1;
 }

  num = num / 2;
  printf("%d",val);
  binval(num);
  return;
}

int get_number() { 
  int value = 0;
  char c;
  printf("Please input number : ");
  while ((c = getchar()) != '\n') { 
    if ( (c>'9') || (c<'0') ) { 
      printf("Incorrect character entered as a number - %c\n",c);
      exit(-1);
    }
    else {
      value = 10*value + c - '0';
    }
  }
  return(value);
}

now it compiles just get 01 for every answer.

+2  A: 

You should declare the prototype of binval above main like void binval(int);

or copy the whole binval function above main.

Raghu

Raghuram
ok, fixing that made it compile, but now I get the wrong binary output, thank you for your help, I will now try to figure out what else is wrong.
pisfire
@pisfire - Some useful hints are given in the answers below. You can make a note of it.
Praveen S
I got it thanks to everyones help. How come it would not work with the declaration void binval(int num); but would work with void binval(int);
pisfire
+1  A: 

You need to declare the function before the main() just like you did int get_number(); or you can define all your functions before main().

This is called forward declaration.

Praveen S
This was just a dumb mistake on my part, just getting tired.
pisfire
+3  A: 

I believe you need to declare your binval function, or the compiler assumes it returns int. Then when you define it, it doesn't match that assumption.

Also, this line will likely give you trouble:

if (test = 1) {

This will assign test to the value 1, but I think you meant to compare it:

if (test == 1) {
Fred Larson
+1  A: 

The call to binval in main implicitly declares it like int binval(int).

In order to fix that, you'll need to either add a forward declaration for binval, or move main to after binval.

After that, and after fixing the other stuff mentioned in other answers, we still have one little problem: the bits will be printed out backwards, because you're printing the least significant bit first. The easiest way to fix that is to switch the binval and printf within the binval function itself, but of course you probably won't want the base case (num==0) to print a newline. Just print a newline after you call the function from main.

cHao
+2  A: 

There are a few problems I can see:

  • Declare the function binval before main as void binval (int num);

  • Function get_number() returns the number read but you are not collecting it in num. So get_number(); should be num = get_number();

  • if (test = 1) should be if (test == 1)

  • Your current logic print the binary value of the number in reverse. You need to fix that.

codaddict
ok I knew I had a problem with num, I will see if that fixes it
pisfire
+1  A: 

You have

void binval (int num) {
  /* ... */
  printf("%d",val);
  binval(num);
  return;
}

which, for 16, prints "00001". Try recursing first and print after.

void binval (int num) {
  /* ... */
  binval(num);
  printf("%d",val);
  return;
}
pmg
I had given up on being able to fix this part. Thank you.
pisfire
Out of my entire junior class only one other person got this. I am very impressed you caught that.
pisfire
I run your code. It was easy to detect the error ... and once I knew that that particular error existed, easy to fix :)
pmg