ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int parse(const char *s) {
int sign = 1, n = 0;
if (*s == '-') { sign = -1; s++; }
while (*s >= '0' && *s <= '9') n = n * 10 + (*s++ - '0');
return sign * n;
}
int main(void) { printf("%d %d", parse("-042x7"), parse("+5")); }- A. -42 5
- B. 0 0
- C. -42 0
- D. -427 0
Correct answer: C. -42 0
The leading zero adds nothing and the loop stops at 'x', so the first is −42 (the 7 after x is never read). parse handles only '-', so for "+5" the loop sees '+' first, reads no digits and returns 0. The library atoi would accept the '+'.
More C Programming questions
- What is printed? c int a[] = {1, 1, 2, 3, 3, 3, 4}, k = 1; for (int i = 1; i < 7; i++) if (a[i] != a[k - 1]) a[k++] = a[i]; printf("%d…
- What is printed? c int a[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int best = a[0], cur = a[0]; for (int i = 1; i < 9; i++) { cur = cur + a[i]…
- What is printed? c int a[] = {2, 4, 6, 8, 10, 12, 14}, lo = 0, hi = 6, steps = 0, x = 7; while (lo <= hi) { int m = (lo + hi) / 2…
- What is printed? c struct E { char n; int age; } e[] = {{'A', 30}, {'B', 25}, {'C', 35}}; int cmp(const void x, const void y) { return…
- What is printed? c #define SCALE(x) (x factor) int factor = 3; int f(void) { int factor = 10; return SCALE(2); } int main(void) {…
- What is printed? c int i = 0, n = 0; while (i++, i < 4) n += i; printf("%d %d", n, i);