I am writing a program to count number of digit and reverse the number but the garbage value is getting printed when assign value to datatype while declaring
#include<stdio.h>
int main()
{
int n=213,t=0,a,e,i=0;
e=n;
while(e!=0)
{
e/=10;
i++;
}
printf("%d",i);
while(n!=0){
t=n%10;
a=a*10+t;
n/=10;
}
printf("%d",a);
return 0;
}
You need to initialize the variable a
with zero, before doing this
a=a*10+t; /* a contains garbage data here, initialize it before */
else a
default contains garbage value due to its automatic storage . For eg
int n=213,t=0,a = 0 /* initialize a with 0 */,e,i=0;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.