1

Please give me some feedback on how to make my code better or more efficient. It should convert a decimal integer to binary.

#include <stdio.h>

binarydigits(int div, int dis)
{
    int numit;
    numit=0;

    do
    {
        ++numit;
        div /= dis;
    }
    while (div!=1);
    ++numit;
    return numit;
}

main()
{
    int x, nb, i;

    printf("\n Input an decimal integer number to be converted: ");
    scanf("%d", &x);
    fflush(stdin);

    if (x==0 || x==1)
    {
        printf("\n\n %d in binary : %d", x, x);
    }

    else
    {
        printf("\n\n %d in binary : ", x);
        nb = binarydigits(x, 2);
        // the function 'binarydigits' returns how many binary digits are needed to represent 'x'
        int remind[nb];
        // an array of 'nb' elements is declared. Each element of this array will hold a binary digit
        for(i=(nb-1) ; i>=0 ; --i, x/=2)
        {
            remind[i] = x%2;
        }
        //this 'for' structure saves the remainder of 'x/2' (x%2) in an element of the 'remind[nb]' array

        for (i=nb ; i>0 ; --i)
        {
            printf("%d", remind[nb-i]);
        }
        //this 'for' structure prints the elements of the 'remind[nb]' array in increasing order

    }

    getch();
    return 0;


}

Any tips on how to make this better would be nice.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Joctã Torres
  • 51
  • 2
  • 6

2 Answers2

1

Firstly, binarydigits should have a return type int. This is because you return an integer variable numit at the end of this function. Change your function header to:

int binarydigits(int div, int dis)

Secondly, the main() function needs to have a return type int by definition in C, and C++ for that matter. Without it, your compiler will produce a warning, something similar to:

main.c:18:1: warning: return type defaults to 'int' [-Wimplicit-int]
 main()
 ^~~~

Here is a snippet from the the C11 standard (ISO/IEC 9899:2011) on the definition of the main() function:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: - Return Type of main()

int main(void) { /* ... */ }

Thirdly, you should remove fflush(stdin) because using the fflush() for stdint is undefined behavior as it is not a part of standard C. From C11 7.21.5.2, fflush works only with output/update stream, not input stream:

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined. - fflush(stdin)

Community
  • 1
  • 1
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • int is the default return type in C – Ryan Mar 28 '17 at 21:55
  • Not including `int` may lead to a compilation error in some compilers, and a warning from every compiler. – BusyProgrammer Mar 28 '17 at 21:57
  • @A Busy Programmer. Thanks. I've changed the return type for binarydigits and main functions. I've been using 'fflush(stdin)' to erase the keyboard buffer. What do you recommend using for erasing the input buffer? – Joctã Torres Mar 28 '17 at 22:20
  • @JoctãTorres Multiple ways to do it. I would recommend visiting this StackOverflow post, it provides some really neat methods for clearing the input stream : http://stackoverflow.com/questions/27273303/alternate-method-for-clearing-input-buffer-in-c – BusyProgrammer Mar 28 '17 at 22:25
0

How to make my code better or more efficient?

My advice to you is to stop trying to learn C by trial-and-error method. You should obtain a good book and study it first. It is impossible to create a fast and efficient C program without mastering pointers, bitwise operators and memory manipulations.

Simply, to make your code fast, you should completelly delete your code (I am not going to list all of your bad-practice things here) and start understanding my example:

int main(void){
  char *s = (char*)malloc(33);
  char *t = s;
  int a;

  s += 33;
  *s = 0;

  printf("Enter a number: ");
  scanf("%d", &a);
  printf("That number in binary: ");
  while(a){
    *(--s) = a & 1 ? '1' : '0';
    a >>= 1;
  }
  printf("%s\n", s);

  free(t);
  return 0;
}

Explanation: we have pointer (if you don't know pointers, well you should probably first learn them) s which points to the end of a string. While number from input (number a) is nonzero, we put its last binary digit in the string, decrease pointer and divide a integrally by 2 (this is a >>= 1 instruction). When a is 0, just print the string.

user7771338
  • 185
  • 6
  • OP was asking for feedback on his own code, not for someone else's code. Also, you haven't described your code at all in your post. – BusyProgrammer Mar 28 '17 at 22:02
  • @ABusyProgrammer. He said "Please give me some feedback on how to make my code better or more efficient", so my answer is that his code should be changed completelly in order to make it fast. He used a lot of bad-practice algorythms. Also, why do you think I should explain my code? Isn't it clear enough? – user7771338 Mar 28 '17 at 22:03
  • Without getting into an argument, your code may not be clear enough for OP. For example, (without intending to put any words in OP's mouth), he may have not learnt to use pointers. Also, he said: "_so any tip how how to make __this__ better would be nice_", which indicates that he wants help on his own code. Its a part of the learning process, doing something yourself and then getting feedback from the community. Providing free code ruins that experience. – BusyProgrammer Mar 28 '17 at 22:07
  • @ABusyProgrammer. Ok, I improved my answer a bit. But, I still think the best method to learn something is trying to understand other's code, not trying to improve bad code. – user7771338 Mar 28 '17 at 22:15
  • Thanks for your feedback. For your concern, i am not using a 'trial-and-error' method. Actually, i am using a really good book. As i've said, i've just started learning C. I'll study pointers next ;) but thanks anyway – Joctã Torres Mar 28 '17 at 22:34
  • @JoctãTorres. I'm glad that my answer helped you (in any way). If you found it useful (like BusyProgrammer's answer), feel free to upvote our answers (now you hve 16 reputatios, so you can do it :D ). – user7771338 Mar 30 '17 at 01:20