ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
const char *s = "a1b2c3d4";
int sum = 0;
for (; *s; s++) {
if (*s >= 'a') continue;
if (*s == '3') break;
sum += *s - '0';
}
printf("%d", sum);- A. 10
- B. 3
- C. 6
- D. 1
Correct answer: B. 3
Letters are skipped by continue (in ASCII, letters have larger codes than digits). The digits 1 and 2 are added; at '3' the loop breaks, so 3 and 4 are never added. '10' ignores the break; '6' adds the 3 before breaking.
More C Programming questions
- What is printed? c int s = 0; for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; case 3: break; default: s += 10; } s += i; }…
- What is printed? c int a[] = {10, 20, 30}; printf("%d", a[1, 2]);
- What is printed? c struct P { int x; int y; } p = {7, 9}; int q = (int )&p; printf("%d", q);
- What is printed? c int p(int n, int m) { / ways to write n as a sum of parts <= m / if (n == 0) return 1; if (n < 0 || m == 0) return 0…
- What is printed? c printf("%#x %#o %x", 255, 8, -1 & 0xFF);
- Assume 8-bit unsigned char. What is printed? c printf("%d %d %d", (int)-3.7, (unsigned char)-1, (int)(0.1 30));