1
// Get card number
long n = get_long("Number: ");
// Count length
int i = 0;
long cc = n;
while (cc > 0)
{
    cc = cc / 10;
    i++;
}

Source: https://medium.com/swlh/cs50-pset-1-credit-c7996fb0a837

It is mentioned:

We can then determine the length of the number by continuously dividing by 10, effectively knocking a digit off the end each time through the while loop until there are no digits left.

What I see is that cc = cc / 10 will keep on running with cc tending towards smaller and smaller fraction but not zero.

Suppose, finally afer cc/10, left with 9. Then 9/10, which is a fraction and not an integer. Seeking insight what happens at this stage.

I understand cc too an integer type (long integer), cc cannot be fraction.

1 Answer 1

2

Review the week 1 video starting at around 1:22:00. From the transcript:

If they are both ints, by definition of how C works, you are going to get back an integer as your answer. So if you do 1 divided by 2. Yes, mathematically that's 0.50000. However, if you convert that to an int, in so far as x and y are ints, the way C works is it truncates everything after the decimal point.

long is a 64-bit integer.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .