In the numberOfDigits
function, I didn't assign a value to the digits
variable because by default it's 0. But then in the printf
statement for digits
variable it got printed as 168.
I got the expected output after I assign zero to the digits
variable. So, my question: is it necessary to assign values for the variables in a user define function? If yes why?
#include <stdio.h>
void numberOfDigits(int num);
void main()
{
int num;
printf("Enter integer :");
scanf("%d",&num);
numberOfDigits(num);
}
void numberOfDigits(int num)
{
int nc=num, digits=0;
while(nc>0)
{
nc=nc/10;
digits++;
}
printf("Number of digits in %d are %d\n",num,digits);
}