[英]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;
}
在上面的程序中,我想打印小於 9 的數字,如果大於 9 應該打印它是舊的還是在 for 循環內。
我是 C 編程的初學者,誰能幫我弄清楚這段代碼有什么問題,為什么它退出循環而不給出任何輸出。
在其中一個控制台中,我收到以下錯誤:
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
我的數組聲明有問題嗎?
不要讓另一個a
對象。
它隱藏了在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.