MOHAMED
MOHAMED

Reputation: 43518

long comparison

I have the following code

long x;
scanf("%ld",&x)
if(x==-1) // does this comparison is allowed
    printf("just test\n");

does long parameters need any casting before comparison?

Upvotes: 0

Views: 1079

Answers (2)

unwind
unwind

Reputation: 399703

As meh said, that's fine. If you want to avoid the "type-anxiety", you can make the literal have type long:

if(x == -1l)
          ^
          |
     lower-case 'L'
    means "long int"

But this is a bit anxious-looking in itself, in some contexts.

Upvotes: 3

Kiril Kirov
Kiril Kirov

Reputation: 38143

-1 is a decimal int . There's an implicit conversion (promotion) from int to long, so -1 is automatically "casted" to long.

Also, both -1 and x are signed types. No need from any additional casts.

Upvotes: 7

Related Questions