0

I have a function that counts how much digits a number has, but when I put a number like "05", it recognizes just as 1 digit, in stead of 2.

    #include <stdio.h>

int compd(int num)
    {
        int count = 0;
        if(num == 0)
        {
            return (1);
        } else
        {
            while(num != 0)
            {
                num = num/10;
                ++count;
            }
            return (count);
        }
    }

main()
{
    int num;
    scanf("%d", &num);
    printf("%d", compd(num));
}    

The program takes the value of the number and divides it by 10 until reaching a non-integer number. But 05 is the same as 5, in practice; dividing 05 just once by 10 it gets a decimal number, as well as 5. How to solve this bug? Thank you.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    It is not a bug. The number 5 independent on how it is written with leading zeroes or not is the number 5 with one digit.:) – Vlad from Moscow Nov 14 '22 at 19:07
  • You can just `return x` there's no need for `return (x)`. `return` is not a function. – tadman Nov 14 '22 at 19:08
  • It should be, at the minimum, `int main()` or better, `int main(int argc, char** argv)`. – tadman Nov 14 '22 at 19:08
  • 3
    If someone puts in `5`, or `05`, or `0000005`, the `int` is still always numeric value **5**. Leading zeros are NOT stored in numeric data types. Same if someone writes `+5`, or `+0005` – abelenky Nov 14 '22 at 19:09
  • Simpler: `int count = 0; do { num /= 10; count++; } while (num != 0); return count;` – Jonathan Leffler Nov 14 '22 at 19:09
  • 1
    assuming all that will be entered is numbers, what you could do to get the result you seem to be looking for is read the input as a string and then just print the length of the string. This would preserve leading zeros and count them – Portal Nov 14 '22 at 19:10
  • 2
    @tadman — since the program doesn't use either `argc` or `argv`, the better alternative is `int main(void)`. – Jonathan Leffler Nov 14 '22 at 19:10
  • @JonathanLeffler Well, it doesn't now, but I'd argue using `argc` and `atoi` is better than `scanf`. – tadman Nov 14 '22 at 19:11
  • 2
    Fun fact: in C, a leading `0` is used for octal representation. e.g. `011 == 9`. – rustyx Nov 14 '22 at 19:12
  • @rustyx That is in fact a "fun" fact. – tadman Nov 14 '22 at 19:12
  • 1
    Are you sure you want to process this as `int` and not `char*` instead? – tadman Nov 14 '22 at 19:13
  • 1
    I'd not disagree that handling arguments would be more useful, @tadman, but that isn't the exercise at the moment. If you compile with appropriate warning/error options, you'll be told that `argc` and `argv` are unused — and it would be better to avoid that. As the code is not currently using the arguments, it should use `int main(void)` for preference and `int main()` as a less satisfactory (but 'mostly harmless') fallback. – Jonathan Leffler Nov 14 '22 at 19:14
  • @JonathanLeffler why include void in the parenthesis rather than not? just curious – Portal Nov 14 '22 at 19:16
  • Are you trying to count the number of digits entered, or are you trying to count the number of digits in the value converted to an `int`? If you enter `05` and want a response of `2`, you'll need to work with the string representation of the number, not the converted value. Once converted, there is no difference between `5` and `05` and `005`. – Jonathan Leffler Nov 14 '22 at 19:16
  • Sometimes it's just a mistake to handle a "number" as an "integer" instead of a "string". Amongst them are: those which you want to reverse; credit cards; phones; house numbering, palindrome tests.... – Weather Vane Nov 14 '22 at 19:17
  • 1
    @Portal — I'll refer you to [What should`main()` return in C and C++?](https://stackoverflow.com/a/18721336/15168) — the last section of my answer there (and one of my [comments](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c#comment82555357_18721336) discusses exactly that question. I should probably add the comment into the answer. – Jonathan Leffler Nov 14 '22 at 19:22

0 Answers0