简体   繁体   English

C - printf 打印随机字符而不是数组内容

[英]C - printf prints random characters instead of array contents

I am making a program that prints the number 0-999 in words, using the if else, or switch, no loops or other complicated stuff.我正在制作一个程序,用单词打印数字 0-999,使用 if else 或 switch,没有循环或其他复杂的东西。 The program prints the integer 433, four hundred forty three.该程序打印整数 433,即四百四十三。 Not just four three three.不只是四三三。 I've made char arrays of the numbers 1-9 so that I can use such template to make the words correct.我已经制作了数字 1-9 的字符数组,以便我可以使用这样的模板来使单词正确。 I think I'm done and I didn't make any mistakes, I even checked it over and over again.我想我已经完成了,我没有犯任何错误,我什至一遍又一遍地检查它。 The problem is it doesn't print the array contents.问题是它不打印数组内容。 It instead prints random characters like @ M y.相反,它会打印诸如@M y 之类的随机字符。

I'm sorry the program is in my native language, satuan means ones, puluhan means tens, and ratusan means hundreds.很抱歉,该程序是用我的母语编写的,satuan 的意思是一个,puluhan 的意思是十,而ratusan 的意思是百。 For example when I input 455, it should output empat ratus lima puluh lima (empat is four and lima is five).例如,当我输入 455 时,它应该输出 empat ratus lima puluh lima(empat 为 4,lima 为 5)。 but it instead prints @ M y ratus ??!@ puluh ??!@, the characters will be random everytime I try it again.但它会打印@ M y ratus ??!@ puluh ??!@,每次我再试一次时,字符都会是随机的。

Can someone tell me what's wrong?有人可以告诉我出了什么问题吗?

#include <stdio.h>
#include <math.h>

int main()
{
    int input, a, b, c;
    char satuan[100], puluhan[100], ratusan[100];
    scanf("%d", &input);
        c = input%10;
        b = (input%100-(input%10))/10;
        a = input/100;
    if(input==0){
        printf("nol\n");
    }
    else if(input==10){
        printf("sepuluh\n");
    }
    else if(input==11){
        printf("sebelas\n");
    }
    else if(input==12){
        printf("dua belas\n");
    }
    else if(input==13){
        printf("tiga belas\n");
    }
    else if(input==14){
        printf("empat belas\n");
    }
    else if(input==15){
        printf("lima belas\n");
    }
    else if(input==16){
        printf("enam belas\n");
    }
    else if(input==17){
        printf("tujuh belas\n");
    }
    else if(input==18){
        printf("delapan belas\n");
    }
    else if(input==19){
        printf("sembilan belas\n");
    }
    else if(input==101){
        printf("seratus satu\n");
    }
    else if(input==102){
        printf("seratus dua\n");
    }
    else if(input==103){
        printf("seratus tiga\n");
    }
    else if(input==104){
        printf("seratus empat\n");
    }
    else if(input==105){
        printf("seratus lima\n");
    }
    else if(input==106){
        printf("seratus enam\n");
    }
    else if(input==107){
        printf("seratus tujuh\n");
    }
    else if(input==108){
        printf("seratus delapan\n");
    }
    else if(input==109){
        printf("seratus sembilan\n");
    }
    else {

        if(c==1){
        char satuan[100] = {'s','a','t','u'};
        }
        else if(c==2){
        char satuan[100] = {'d','u','a'};
        }
        else if(c==3){
        char satuan[100] = {'t','i','g','a'};
        }
        else if(c==4){
        char satuan[100] = {'e','m','p','a','t'};
        }
        else if(c==5){
        char satuan[100] = {'l','i','m','a'};
        }
        else if(c==6){
        char satuan[100] = {'e','n','a','m'};
        }
        else if(c==7){
        char satuan[100] = {'t','u','j','u','h'};
        }
        else if(c==8){
        char satuan[100] = {'d','e','l','a','p','a','n'};
        }
        else if(c==9){
        char satuan[100] = {'s','e','m','b','i','l','a','n'};
        }


            if(b==1){
            char puluhan[100] = {'s','a','t','u'};
            }
            else if(b==2){
            char puluhan[100] = {'d','u','a'};
            }
            else if(b==3){
            char puluhan[100] = {'t','i','g','a'};
            }
            else if(b==4){
            char puluhan[100] = {'e','m','p','a','t'};
            }
            else if(b==5){
            char puluhan[100] = {'l','i','m','a'};
            }
            else if(b==6){
            char puluhan[100] = {'e','n','a','m'};
            }
            else if(b==7){
            char puluhan[100] = {'t','u','j','u','h'};
            }
            else if(b==8){
            char puluhan[100] = {'d','e','l','a','p','a','n'};
            }
            else if(b==9){
            char puluhan[100] = {'s','e','m','b','i','l','a','n'};
            }


                if(a==1){
                char ratusan[100] = {'s','a','t','u'};
                }
                else if(a==2){
                char ratusan[100] = {'d','u','a'};
                }
                else if(a==3){
                char ratusan[100] = {'t','i','g','a'};
                }
                else if(a==4){
                char ratusan[100] = {'e','m','p','a','t'};
                }
                else if(a==5){
                char ratusan[100] = {'l','i','m','a'};
                }
                else if(a==6){
                char ratusan[100] = {'e','n','a','m'};
                }
                else if(a==7){
                char ratusan[100] = {'t','u','j','u','h'};
                }
                else if(a==8){
                char ratusan[100] = {'d','e','l','a','p','a','n'};
                }
                else if(a==9){
                char ratusan[100] = {'s','e','m','b','i','l','a','n'};
                }
    }

            if(a==0 && b==0){
                printf("%s", satuan);
            }
            else if (a==0){
                printf("%s puluh %s", puluhan, satuan);
            }
            else{
                printf("%s ratus %s puluh %s", ratusan, puluhan, satuan);
            }

    return 0;
}

The problem is, in every if...else condition body, the variable satuan is (re)defined locally, and the scope is only till the end of the body.问题是,在每个if...else条件体中,变量satuan是在本地(重新)定义的,并且范围仅到主体的末尾。 The variable which you're trying to print, remains uninitialized, and the content of that is indeterminate.您尝试打印的变量保持未初始化状态,其内容不确定。

Change the assignments to将分配更改为

 strcpy(satuan, "whatever value");

Lets take, for example, this code here:让我们以这里的代码为例:

if(c==1){
char satuan[100] = {'s','a','t','u'};
}

Here you define a second variable named satuan , inside the scope of the if statement block and only in the scope of the if statement block.在这里,你定义一个名为第二个变量satuan的范围内if语句块,在范围if语句块。

Once this block ends (with the closing } ) this variable ceases to exist, its life-time ends.一旦这个块结束(以结束} ),这个变量就不再存在,它的生命周期结束。

This leaves you with the first satuan array, which isn't initialized.这给您留下了第一个未初始化的satuan数组。

Same with all the other array definitions.与所有其他数组定义相同。

The solutions is to use the existing satuan array, and copy the strings you want to print into it:解决方案是使用现有的satuan数组,并将要打印的字符串复制到其中:

if(c==1){
    strcpy(satuan, "satu");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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