Home > Software design >  Why is this program exiting the loop without printing any output?
Why is this program exiting the loop without printing any output?

Time:11-06

#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.

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.

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?

CodePudding user response:

Don't make another a object.

It hides the higher level a declared in int a, b;.

// for(int a;a<=b;a  ){
for( ; a<=b; a  ){
  • Related