ProgrammingVery hardProduct company OA
What is printed?
What is printed?
/* 8-bit unsigned char, 32-bit int */
unsigned char c = 0x80;
printf("%d %d %d %d", c << 1, (unsigned char)(c << 1), ~c, c >> 8);- A. 0 0 127 0
- B. 256 0 -129 0
- C. 256 0 127 0
- D. 256 256 -129 0
Correct answer: B. 256 0 -129 0
Before any arithmetic, an unsigned char is promoted to int. So c << 1 is the int 256 (no 8-bit overflow), and only the explicit cast back to unsigned char wraps it to 0. ~c complements all 32 bits of the int 128, giving −129, not 127. c >> 8 is 128 >> 8 = 0.
More C Programming questions
- What is printed? c char s[] = "a,b,,c"; int n = 0; for (char t = strtok(s, ","); t; t = strtok(NULL, ",")) n++; printf("%d %zu %c", n…
- What is printed? c char s[] = "Hello"; s[0] = 'J'; s[4] = '0'; printf("%s %zu", s, sizeof(s));
- What is printed? c int i = 5; printf("%d ", i++); printf("%d", ++i);
- What is printed? c printf("%.2f %d %d", 7 / 2.0, 7 / 2, 7 % 2);
- What is printed? c int x = 5; void f(void) { int x = 1; x++; } void g(void) { x++; } int main(void) { f(); g(); g(); printf("%d", x); }
- What is printed? c char s = "programming"; int c = 0; while (s) if (s++ == 'm') c++; printf("%d %c", c, (s - 1));