Counting digits using while loop

前端 未结 7 606
清酒与你
清酒与你 2021-01-05 10:15

I was recently making a program which needed to check the number of digits in a number inputted by the user. As a result I made the following code:

int x;            


        
7条回答
  •  甜味超标
    2021-01-05 10:38

    Bar the suggestions of reading the number as a string, your current method of counting the number of significant decimal digits is fine. You could make it shorter, but this could arguably be less clear (extra set of parenthesis added to keep gcc from issuing warnings):

    while((x = x/10))
      count++;
    

提交回复
热议问题