C scanf()返回-1

aho*_*ota 0 c user-input scanf

int input;
printf("Type in an odd number less than or equal to 9: \n");

int correctInput = 0;
do {
    scanf("%d", &input);
    if((input % 2) == 0) {
        printf("You have not entered an odd number. Please try again. \n");
    }
    else if(input > 9 || input < 1) {
        printf("Your input is not from 1 to 9. Please try again. \n");
    }
    else {
        correctInput = 1;
    }
} while(correctInput == 0);

printf("Input: %f. \n", input);
Run Code Online (Sandbox Code Playgroud)

我想做的就是从输入变量的1-9得到一个奇数.但是,当我运行此代码并输入类似7的内容时,我得到了

Type in an odd number less than or equal to 9:
3
Input: -1.#QNAN0.
Run Code Online (Sandbox Code Playgroud)

oua*_*uah 5

printf("Input: %f. \n", input);
Run Code Online (Sandbox Code Playgroud)

改用它:

printf("Input: %d. \n", input);
Run Code Online (Sandbox Code Playgroud)

f转换说明符是打印double值,使用d转换说明符来打印int值.