3

I'm very new to C, so I don't know much about it. Pointers are something I still haven't learned... I need to show a single character in decimal and binary. In decimals its easy but I can't get it to binary in any way... I get the ASCII number and compare it's module with 2 but it shows a really awkward number... what is wrong with the code?(It's in portuguese This is my code:

#include <stdio.h>
#include <conio.c> //library my professor created to assist his students
#include <string.h>
#define MAXIMO 8

int main( void )
{
  char caracter;
  int aux, resto[MAXIMO], contador, decimal, numero;
  printf( "Digite um UNICO caracter: " );
  caracter = getch();
  numero = caracter;
  decimal = caracter;
  aux = 0;
  do
  {
    if (numero % 2 == 0)
    {
      resto[aux] = 0;
    }
    else
    {
      resto[aux] = 1;
    }
    numero = numero / 2;
    aux++;

  }
  while (numero >= '0');
  //system("cls");
  printf( "\n\n%d\n\n", aux );
  printf( "\nCaracter em Binario: " );
  for (contador = aux; contador >= 0; contador--)
  {
    printf( "%d", resto[contador] );
  }
  printf( "\nValor decimal do caracter: %d", decimal );
  getch();
  return 0;
}

somethings I use like getch(); and return 0; in the end are just so my teacher don't kill me... I don't fully know why it's there but it has to be.

5
  • For starters: contador = aux needs to be contador = aux-1; as aux is one past the last index that was set in the while loop above that. Commented Oct 12, 2016 at 20:05
  • while(numero >= '0'); is also wrong. '0' and 0 are different values. You want the latter. Commented Oct 12, 2016 at 20:09
  • @kaylum if I use contador = aux-1 it only prints 7 numbers and doesn't fill the last one with a 0, that's why I used aux. I saw this '0' error too, thanks. Commented Oct 12, 2016 at 20:12
  • 2
    About your last sentence: the getch(); is there so that if the console output is to a popup window it won't just disappear before you can read any output. The return 0; is there because the function type is int (although main is an exception, it is not mandatory in this case to return a value). Commented Oct 12, 2016 at 20:35
  • @WeatherVane Thanks!!! Commented Oct 12, 2016 at 21:29

1 Answer 1

1

Your code had some errors in the while condition:

#include <stdio.h>
#include <string.h>
#define MAXIMO 8

int main( void )
{
  char caracter;
  int aux, resto[MAXIMO], contador, decimal, numero;
  printf( "Digite um UNICO caracter: " );
  caracter = getchar(); // I used getchar instead of getch()
  numero = caracter;
  decimal = caracter;
  aux = 0;
  do
  {
    if (numero % 2 == 0)
    {
      resto[aux] = 0;
    }
    else
    {
      resto[aux] = 1;
    }
    numero = numero / 2;
    aux++;

  }
  while (numero > 0); // > than 0 not >= '0' 
  //system("cls");
  printf( "\n\n%d\n\n", aux );
  printf( "\nCaracter em Binario: " );
  for (contador = aux-1; contador >= 0; contador--) //aux-1
  {
    printf( "%d", resto[contador] );
  }
  printf( "\nValor decimal do caracter: %d", decimal );
  getchar();
  return 0;
}

N.B.

  • '0' and 0 are differents
  • > (greater) and >= (greater or equal) have different meaning
Sign up to request clarification or add additional context in comments.

2 Comments

I've made some changes to my code in which using 7 instead of aux or aux-1 solves the problem of numbers only showing 7 characters I had before. I used getch because I want only one character leaving no room for user mistakes, if I use any other function it lets the user enter more and I'll have to validate the answer.
@ViníciusBreda you can use getch() if you want, but is not a function of standard c. It's in a library made for windows os.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.