PC Luddite
PC Luddite

Reputation: 6098

Facing segmentation fault while using toupper()

The following method causes an error:

BOOL should_begin(void) {
    char yn;
    do {
        printf("Continue? [Y/N] ");
        yn = getchar();
        printf("\n");
        yn = toupper(yn);
        if (yn == 'N') {
            return FALSE;
        }
    } while (yn != 'Y');
    return TRUE;
}

The code executes normally until toupper() is reached, at which point there is a segmentation fault. I've seen questions like this where toupper() was called on part of a string, but it was only when someone tried to modify a literal.

So what gives? char yn shouldn't be read only, right? It's just a char, a single byte of data, I'm not reading a whole string, am I?


EDIT:

This is my main() function.

int main(int argc, char* argv[]) {

    /* just some printf()s with instructions */

    if (!should_begin()) {
        return 0;
    }

    /* then continue with rest of the program */

    return 0;
}

Upvotes: 0

Views: 419

Answers (1)

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

getchar() returns int. Some of the return values may not fit into a char.

Change yn to int yn.

Then, from the man page for toupper()

int toupper(int c);

If c is not an unsigned char value, or EOF, the behavior of these functions is undefined.

So, you need to check for EOF before passing yn to toupper().

FWIW, the toupper() is prototyped in ctype.h, you have to #include the same.

Upvotes: 4

Related Questions