ProgrammingVery hardTCS NQT · Infosys
What is printed? Assume uint8t is 8 bits and unsigned int is 32 bits.
What is printed? Assume uint8_t is 8 bits and unsigned int is 32 bits.
#include <stdint.h>
#include <stdio.h>
uint8_t sat(uint8_t a, uint8_t b) {
unsigned s = a + b;
return s > 255 ? 255 : (uint8_t)s;
}
int main(void) {
printf("%d %d", sat(200, 100), sat(100, 50));
}- A. 255 150
- B. 44 150
- C. 255 44
- D. 255 255
Correct answer: A. 255 150
The sum is computed in unsigned int, so the test s > 255 detects saturation before any wrap-around. 100 + 50 = 150 fits and is returned as is.
More C Programming questions
- What is printed? c #include <stddef.h> #include <stdio.h> int main(void) { sizet a = 3, b = 5; printf("%d", (a - b) / 2 > 0); }
- What is printed? c #include <stdio.h> int od(unsigned n); int ev(unsigned n) { return n == 0 ? 1 : od(n - 1); } int od(unsigned n) {…
- A complete binary tree of 7 nodes is stored in an array with children of node i at 2i+1 and 2i+2. The values are v = {5, 3, 8, 1, 4, 9…
- What is printed? c #include <stdio.h> #include <stdlib.h> struct Pair { int a, b; }; int cmp(const void p, const void q) { const struct…
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void a, const void b) { const char x = (const…
- How many subarrays of {2, 1, 4, 3, 1, 2, 4} have a sum of exactly 7? Compute with this program. c #include <stdio.h> int main(void) { int…