简体   繁体   English

当我在 c 中添加具有值的变量时获取垃圾值,同时查找位数并打印数字的反转

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM