ProgrammingVery hard
char *a = "", *b = "abc"; if (strlen(a) - strlen(b) > 0) puts("longer"); else puts("not longer"); What is printed?
- A. not longer
- B. longer
- C. nothing, because the program does not compile
- D. undefined behaviour
Correct answer: B. longer
strlen returns size_t, so 0 - 3 wraps around to a huge positive value and the test is true. Unsigned subtraction never produces a negative result.
More C Programming questions
- In C (not C++), what is sizeof('A')?
- char a[5] = "hi"; printf("%d", a[4]); What is printed?
- int n = -1; if (n < strlen("abc")) puts("yes"); else puts("no"); What is printed?
- int v = 0; for (const char p = "-42"; p >= '0' && p <= '9'; p++) v = v 10 + (p - '0'); printf("%d", v); What is printed?
- 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?