#include <stdio.h>
int countDigits(int n){
    static int count = 0;
        return count++;
        // return 2;
    return count + countDigits(n/10);

int main(void){
    int num;
    printf("Give the number: ");
    int count = countDigits(num);
    printf("The number of digits when return is count++ for 'n==0' is: %d\n",count);
    // printf("The number of digits when return is 2 for 'n==0' is: %d\n",count);

1 Answer 1


The problem lies here:

    return count++;

Remember how the ++ function works. When ++ follows the var name, it increments after the line of code executes. It only increments first if the ++ precedes the variable name. So, this would work here:

    return ++count;

This doesn't address any other issues with the code though. That would be a new question. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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