ProgrammingHardGATE CS · Campus placements
int a = -1; unsigned int b = 1u; What does if (a < b) evaluate to?
- A. true, because -1 is less than 1
- B. false, because a is converted to unsigned int and becomes UINT_MAX
- C. a compile-time error
- D. undefined behaviour
Correct answer: B. false, because a is converted to unsigned int and becomes UINT_MAX
In a relational comparison the usual arithmetic conversions turn -1 into the unsigned value UINT_MAX (for a 32-bit unsigned int, 4294967295). That is larger than 1, so the test is false. The conversion is well defined.
More C Programming questions
- int a = -7, b = 2; printf("%d %d", a / b, a % b); prints what?
- #define SQUARE(x) xx. What does printf("%d", SQUARE(2+3)) print?
- #define MAX(a,b) ((a)>(b)?(a):(b)). With int i = 7; int m = MAX(i++, 5); what are m and i afterwards?
- qsort is called on int arr[5] = {-7, 3, -2, 5, -4} with a comparator that returns (abs(a) > abs(b)) - (abs(a) < abs(b)) on the two…
- A qsort comparator is written as return (const int )a - (const int )b; Which statement is true?
- Which statement about the standard library function qsort is true?