0

I am trying to find the number of digits in the input integer. But it always prints "no. of digits: 1". In place of getting integer i from function, if I use "static int i = 0;" inside the function it works perfectly.
And I can't understand this behavior.

#include <stdio.h>

int func(int a, int i)    
{  
    if (a != 0) 
    {
        i++;
        func(a / 10, i);
    }

    return i;   
}

int main()
{
    int a, c;
    printf("Enter the No:");
    scanf("%d", &a);
    c = func(a, 0);
    printf("No. of digits: %d", c);
    return 0;    
}
  • 3
    You are not using the return value of the recursive `func` call. You need to. By changing it to `static` the variable has lifetime beyond the lifetime of the function. That is, it is the same variable for all invocations of the function. (And it hides the `i` passed into the function). – kaylum Apr 26 '22 at 05:32
  • What is `func` supposed to do? It's hard to understand what "works perfectly" or "doesn't work" mean, when we have no idea what it's supposed to do – Stef Apr 26 '22 at 08:56
  • I think the OP intended for `func(a, 0)` to return the number of decimal digits in A, roughly speaking. – David Grayson Apr 27 '22 at 00:52

2 Answers2

0

Your recursion repeatedly refreshes the memory for your function at every call. ts the same, it is like the same question between global variable and local variable. When you do not use static, every call to your function will refresh your variable i and initialize it again. When you use static, the variable i will be the same as a global variable with just once statement.

Deusy94
  • 733
  • 3
  • 13
0

Regarding your question about static:

It is unclear where you want to use static int i = 0; as alternative. Inside the function, (and removing the function arg i) it becomes a variable that is kept in memory even after the function exits. See this question for reference. If it is outside of a function look at this question for reference.

Regarding your code: Your code is not using the result of your recursive function call. It is neither stored inside the local variable i, nor returned directly. It is simply lost. See a code example below, that might be want you want.

EDIT: The variable i in the original answer was unnecessary, so I'll remove it.

#include <stdio.h>

int func(int a) {
    if (a == 0) {
        return 0;
    }
    return 1 + func(a / 10);
}

int main() {
    int a, c;
    printf("Enter the No:");
    scanf("%d", &a);
    c = func(a, 0);
    printf("%d", c);
    return 0;
}
knmiecc
  • 51
  • 4
  • Now this should be stored somewhere but still problem exist int func(int a, int i) { if (a == 0) { return 0; } i++; func(a / 10, i); return i; } – neelanchaljain Apr 26 '22 at 07:42
  • I'm not entirely understanding what problem you have. If I run that code on my laptop and enter e.g. the value 9992 via the console, I get 4. What problem are you referring to? – knmiecc Apr 26 '22 at 08:30
  • Since the `i` argument in this answer is set but never read, it can be removed. – David Grayson Apr 27 '22 at 00:53
  • Ah right I see. Yeah, the I will edit the response. – knmiecc Apr 27 '22 at 08:40