in Programming in C edited by
1,243 views
0 votes
0 votes

in Programming in C edited by
by
1.2k views

2 Answers

4 votes
4 votes
Best answer

On my Compiler { Intel - Little Endian }, I have the size of Int and Long equal .

However, it is not universal and it may vary on different platforms .

#include <stdio.h>
#include <limits.h>

int main()
{
    signed long int v1;
    signed int v2;

    printf("%d\n",sizeof(v1));  // 4
    printf("%d\n",sizeof(v2));  // 4

    printf("%u\n",UINT_MAX);   // 4,294,967,295
    printf("%lu\n",ULONG_MAX); // 4,294,967,295

    if(-1L < 1U)
	    printf("I am Happy\n");
	if(-1L < 1UL)
	    printf("I am Healthy");
    return 0;
}

According to the integer promotional rules,

Int $\rightarrow$ Unsigned Int $\rightarrow$ Long $\rightarrow$ Unsigned Long


Both the IF blocks are identical for my compiler, as the size doesn't matter. 

Hence, in the general, we are comparing Signed < Unsigned 

Signed is promoted to unsigned and it will be a big number as size is $32$ bits. Comparing that less than $1$ is false, so both the blocks are never executed. 

Hence, Nothing is printed .


EDIT :- When we use this compiler https://www.tutorialspoint.com/compile_c_online.php then output is I am Happy

As in this compiler, size of int = $4$B and size of long = $8$B. Hence, in the first IF block, the comparison is like

if(-1L < 1L).

Now, this returns true and I am happy is printed.


NOTE :- If both of them(int and long) have equal size, then always promote both of them to the largest (Unsigned Long Int).

selected by
by

4 Comments

@Srestha

-1L is not equal to -1UL

Infact, Unsigned is always +ve.

On my compiler, nothing gets printed and if on any platform size mismatch occurs, then, I am happy will be the output .
0
0

NOTE :- If both of them(int and long) have equal size, then always promote both of them to the largest (Unsigned Long Int).

equal size means means in if both have same lower and upper bound?? if yes then if i convert both of them to unsigned int instead of unsigned long int(as if both are of equal size) will not be valid??  

0
0
@2018

Conversion to largest size is a rule in C99 or so only when int and long cannot be distinguished based on the size. But, if size is different, then promote smaller one to the larger one in the comparison .
1
1
2 votes
2 votes

Ans is A

1 comment

On your compiler, int = $4$B and long = $8$B

As sizes are different, that's why I am happy is printed otherwise Nothing is printed .

1
1

Related questions

0 votes
0 votes
3 answers
1