简体   繁体   English

为什么这个程序退出循环而不打印任何输出?

[英]Why is this program exiting the loop without printing any output?

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

int main()
{
    int a, b;
    scanf("%d\n%d", &a, &b);
    for (int a; a <= b; a++) {
        if (a <= 9) {
            char* arr[10] = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
            printf("%s", arr[a - 1]);
        }
        else if ((a > 9) && (a % 2 == 0)) {
            printf("even");
        }
        else if ((a > 9) && (a % 2 != 0)) {
            printf("odd");
        }
    }
    return 0;
}

In above program, I wanted to print the number if it is less than 9 and if it's more than nine should print if it is old or even inside a for loop.在上面的程序中,我想打印小于 9 的数字,如果大于 9 应该打印它是旧的还是在 for 循环内。

I'm a beginner in C programming, can anybody help me figure out what is the issue with this code, why it is exiting the loop without giving any output.我是 C 编程的初学者,谁能帮我弄清楚这段代码有什么问题,为什么它退出循环而不给出任何输出。

In one of the console I got the following error:在其中一个控制台中,我收到以下错误:

Reading symbols from Solution...done.
[New LWP 674234]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65

Is something wrong in my array declaration?我的数组声明有问题吗?

Don't make another a object.不要让另一个a对象。

It hides the higher level a declared in int a, b;它隐藏了在int a, b;声明的更高级别a int a, b; . .

// for(int a;a<=b;a++){
for( ; a<=b; a++){

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

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