ProgrammingHard
int v = 0; for (const char *p = "-42"; *p >= '0' && *p <= '9'; p++) v = v * 10 + (*p - '0'); printf("%d", v); What is printed?
- A. Undefined behaviour
- B. 0
- C. 42
- D. 4
Correct answer: B. 0
The loop stops at the first character that is not a digit. '-' is not a digit, so the body never runs and v stays 0.
More C Programming questions
- char d[4]; strncpy(d, "abcdef", 4); / d now has no terminator / Which call is safe to print d with?
- char s[] = "x=1;y=2"; char t = strtok(s, "="); printf("%s %zu", t, strlen(s)); What is printed?
- Given char names[] holding strings, which qsort comparator sorts them alphabetically?
- char d[6] = "abc"; / we want strncat(d, "defgh", 5) with no overflow / What is the smallest size d needs?
- char b[16]; sprintf(b, "%d%%", 50); printf("%zu", strlen(b)); What is printed?
- printf("%c", 321); What is printed?