[英]getting garbage value when I add variable with value in c while finding number of digit and printing reverse of a number
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=a*10+t; /* a contains garbage data here, initialize it before */
else a
default contains garbage value due to its automatic storage .还有a
默认包含由于其自动存储垃圾值。 For eg例如
int n=213,t=0,a = 0 /* initialize a with 0 */,e,i=0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.