简体   繁体   中英

C Homework, Print out the binary value of an integer

 #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.

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) {

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

or copy the whole binval function above main.

Raghu

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.

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 .

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 .

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM