Why is my C function returning the incorrect int?

时间:2015-06-26 10:04:06

标签: c char printf ascii atoi

I'm making a simple program that asks the user for minimum and maximum values (between 32 and 127, inclusive), but for some reason every time I try to store the minimum value it gets replaced by another value.

Here is the code:

#include <stdlib.h>
#include <stdio.h>
#define lenLimit 256

char text[lenLimit];

int enterNumber(int loLimit, int hiLimit) {
    printf("Please enter an integer between 32 and 127: ");
    fgets(text, lenLimit, stdin);
    int enter = atoi(text);
    int exit;
    if (enter < 32 || enter > 127) {
        printf("Min %i OUT OF range\n", enter);
        enterNumber(enter, hiLimit);
    }
    if (enter >= 32 && enter <= 127 && hiLimit > 127) {
        exit = atoi(text);
        printf("Min %i IN range\n", exit);
    }
    if (loLimit >= 32 && loLimit <= 127 && hiLimit <= 127 && enter >= loLimit) {
        exit = hiLimit;
        printf("Max %i IN range\n", exit);
    }
    printf("num returned: %i\n", exit); //prints twice, why?
    return exit;
}

void printTable(int loLimit, int hiLimit) {
    for (int i = loLimit; i <= hiLimit; i++) {
        printf("ASCII character%3d is %c.\n", i, i);
    }
}

int main(int argc, char* argv[]) {
    int min = enterNumber(31,128);
    printf("Min: %i\n", min);
    int max = enterNumber(min, 128);
    printf("Max: %i\n", max);
    printTable(min, max);
    return(EXIT_SUCCESS);
}

And here is the result:

Please enter an integer between 32 and 127: 31
Min 31 OUT OF range
Please enter an integer between 32 and 127: 33
Min 33 IN range
num returned: 33
num returned: 1600375832
Min: 1600375832
Please enter an integer between 32 and 127: 45
Min 45 IN range
num returned: 45
Max: 45

3 个答案:

答案 0 :(得分:6)

You are calling enterNumber from itself in one of the conditions, the result of which you are discarding. Then you are outputting an unitialised value of exit. (Use of an uninitialised variable is undefined behaviour in C).

Avoid recursion for things like this, use a loop instead.

答案 1 :(得分:3)

您没有初始化&#39;输入&#39;任何事情,它只在你的3个条件中的2个中分配。

此外,您以递归方式调用此函数(调用自身),但在执行此操作时您不会返回。也许这是故意的,但是你在printf后的评论让我想不到。这种递归是你看到2张照片的原因。

试试这个......

int enterNumber(int loLimit, int hiLimit) {
    printf("Please enter an integer between 32 and 127: ");
    fgets(text, lenLimit, stdin);
    int enter = atoi(text);
    int exit = enter;
    if (enter < 32 || enter > 127) {
        printf("Min %i OUT OF range\n", enter);
        return enterNumber(enter, hiLimit);
    }
    if (enter >= 32 && enter <= 127 && hiLimit > 127) {
        printf("Min %i IN range\n", exit);
    }
    if (loLimit >= 32 && loLimit <= 127 && hiLimit <= 127 && enter >= loLimit) {
        exit = hiLimit;
        printf("Max %i IN range\n", exit);
    }
    printf("num returned: %i\n", exit); //prints twice, why?
    return exit;
}

答案 2 :(得分:1)

Below function is called at first.

int min = enterNumber(31,128);

And, enterNumber() is called in enterNumber() again. But This code don't get the result of enterNumber(). So, uninitialized value 1600375832 is shown.