C Program to Count Digits in an Integer

C Program to Count Digits in an Integer

A school number puzzle needs count integer digits for a nonnegative decimal value. Work on a copy so the original remains available for the final decision.

Count decimal digits by removing one trailing digit at a time; zero has one digit.

Before this exercise, review integer division and remainders and loops. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why count integer digits uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use 0, trace the digits and predict the reported result.

Count Integer Digits: follow the data

  1. Given data

    A school number puzzle needs count integer digits for a nonnegative decimal value. Work on a copy so the original remains available for the final decision.

  2. Rule

    Count decimal digits by removing one trailing digit at a time; zero has one digit.

  3. Observed result

    Digits: 3

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

Initialize the count to one so zero and one-digit values are handled. Each division while remaining is at least ten removes one additional digit.

This is a numeric value exercise. Leading zeros in a printed identifier belong to text and are not preserved by an integer representation.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  unsigned value = 120, remaining = value, count = 1;
  while (remaining >= 10) { remaining /= 10; count++; }
  printf("Digits: %u\n", count);
  return 0;
}

Expected output

Digits: 3
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  unsigned value = 120, remaining = value, count = 1;
  while (remaining >= 10) { remaining /= 10; count++; }
  printf("Digits: %u\n", count);
  return 0;
}

Start with the original value 120 and a separate working copy.

The digit operations produce the reported result: Digits: 3.

Follow the worked calculation

  1. Start with the sample

    Start with the original value 120 and a separate working copy.

  2. Apply the rule

    The digit operations produce the reported result: Digits: 3.

  3. Check the result

    Digits: 3

    Digits: 3
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  unsigned value = 120, remaining = value, count = 1;
  while (remaining >= 10) { remaining /= 10; count++; }
  printf("Digits: %u\n", count);
  return 0;
}

Use 0, trace the digits and predict the reported result.

Worked answer

The changed input produces Digits: 1. The explicit initial count preserves the zero case.

C17 · complete program

#include <stdio.h>
int main(void) {
  unsigned value = 0, remaining = value, count = 1;
  while (remaining >= 10) { remaining /= 10; count++; }
  printf("Digits: %u\n", count);
  return 0;
}

Expected output

Digits: 1
Run in C compiler →

Recognize the important boundaries

These fixtures stay below 10000. A general reversed value or square can overflow even when the input itself fits; validate the chosen domain or check each multiply/add.

Negative inputs need an explicit sign policy. Do not assume negating the minimum signed integer is representable.

Check your understanding

Choose the answer, then compare the reasoning for every option.

How many decimal digits does zero have?
Choose one answer. You can try again.

Answer and explanation

One.

Correct. The operation’s boundary and original-value contract must be maintained.

Use the idea independently

Count decimal digits by removing one trailing digit at a time; zero has one digit.

Continue with separate stored values.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Check a Neon Number →