DarkoNaito_09
DarkoNaito_09

Reputation: 101

Comparing unsigned char with integer literal

#include <stdio.h>

int main()
{
    unsigned char voto;

    printf("Inserire il voto (da 2 a 10): ");
    scanf("%d", &voto);

    printf("\n\n\n%d ", voto);

    if(voto > 1 && voto < 11)
    {
        printf("corrisponde al giudizio ", voto);

        switch(voto)
        {
            case 2:
            case 3:
            case 4:
            case 5:
                printf("INSUFFICIENTE");
                break;
            case 6:
                printf("SUFFICIENTE");
                break;
            case 7:
                printf("DISCRETO");
                break;
            case 8:
                printf("BUONO");
                break;
            case 9:
                printf("MOLTO BUONO");
                break;
            case 10:
                printf("OTTIMO");
                break;
        }
    } else
    {
        printf("non e' un voto valido!");
    }

    printf("\n\n----------------------------");


    return 0;
}

I'll copy the the message my teacher sent me:

When the IF compares the CHAR and the numeric constant, two things can happen: either the numeric constant gets converted into an UNSIGNED CHAR and gets compared with "voto" alphabetically, or "voto" gets converted into an INT and it gets compared with the literal numerically. In both cases you force the processor to perform casting and, in the first case, the program may not work correctly: Alphabetically, this would be a correct order: 1 11 111 2 222 23 Here 2 results bigger than 11 and 111, and 23 results bigger than 222.

Doesn't this comparison always cause VOTO to be casted into INT, and not the other way? And, if the literal gets cnverted to UNISGNED CHAR, wouldn't the program work the same way?

Upvotes: 0

Views: 411

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

For starters the call of scanf is incorrect because there is used incorrect conversion specifier %d with an object of the type unsigned char.

unsigned char voto;
//...
scanf("%d", &voto);

Even if you will cast an integer constant to the type unsigned char the usual arithmetic conversions will be applied to the both operands of a relational operation or of the comparison. It means that in the case when the integer constant has the type int or after casting the integer constant to the type unsigned char the both operands will be converted to the type int due to the integer promotions (that is a part of the usual arithmetic conversions) and the integer values of the operands will be compared.This has nothing common with the alphabetical comparison because the integer constant can have any value and it is evident that for example the ASCII value of the character 'A' equal to 65 is less than the integer constant 111.

Upvotes: 2

rici
rici

Reputation: 241931

If that's exactly the program you submitted, your teacher isn't reading your code very well. There is quite a different problem.

Had you written scanf("%c", &voto); then your teacher's comments would make a certain amount of sense, although they seem to be implying that C might cast the character '1' into the value 1, which is certainly not the case. If you really only expected a single character, you could get away with that, although you'd have to change all the other values you compare voto with to character literals, since the value of the character '1', for example, is 49. Anyway, that's not going to work because you clearly require the ability to read the number 10, which is two characters long.

But you are correct that the unsigned char will be converted to an int.

In your actual program, or at least the one pasted here, you've actually written

scanf("%d", &voto);

and that is certainly not correct. The %d conversion specifier must have an a pointer to an int as its corresponding argument; since it does not, it's Undefined Behaviour. In practice, it's likely that scanf will treat the given pointer as being a pointer to a non-existent int, and that means that local variables which happen to be in the vicinity of voto will be overwritten. (Worse things could happen; that's just to give you an idea.)

Upvotes: 3

Related Questions