Please explain comparison of negative number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please explain comparison of negative number

#include <stdio.h> int main(){ int a=10, b =13, d=13, k=-1, c; c= sizeof(long double) < -3-k || b==d && b<a; printf("%d", c); return 0; }

4th Jul 2022, 5:35 AM
Akshay Bhujbal
10 Answers
+ 4
The code is only evaluating 'sizeof(long double) < -3-k' this returns true, or 1. The rest is ignored because there is no need to check because of how '||' works. sizeof returns an unsigned number so -3-k has to be converted to an unsigned number as well. Since -3-k is a negative number the resulting non-negative number is a very big number and the size of a long double is of course less than that big number.
4th Jul 2022, 7:59 AM
Dennis
Dennis - avatar
+ 2
Negative values, and any non-zero values in general, are treated as true when used as conditions C= size(long double) < (-3-k || b==d && b<a); ->&& operator return true if both condition are true Hence b<a is false -> || condition return true if one of statement is true -3-k is negative and non zero number so it is treated true True ->1
4th Jul 2022, 5:45 AM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
Compare that with sizeof(long double )
4th Jul 2022, 5:48 AM
Akshay Bhujbal
+ 2
Akshay, What are you trying to calculate here?
4th Jul 2022, 6:32 AM
Ipang
+ 2
Akshay Bhujbal sizeof(long double) = 16 16<1 :: false.. So answer is zero you put ques like in bracket
4th Jul 2022, 6:40 AM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
Answer is 1 you can run it
4th Jul 2022, 6:42 AM
Akshay Bhujbal
+ 2
Akshay Bhujbal ya but without bracket you can check it .. Further now you got the output..
4th Jul 2022, 6:48 AM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
Okay, thank you for the explanation
4th Jul 2022, 8:04 AM
Akshay Bhujbal
+ 1
The question was find the output
4th Jul 2022, 6:33 AM
Akshay Bhujbal
+ 1
Question was not asked with bracket
4th Jul 2022, 7:01 AM
Akshay Bhujbal