ProgrammingHardTCS NQT · Infosys pattern

Assume a 64-bit LP64 ABI. What is printed?

Assume a 64-bit LP64 ABI. What is printed?
char str[] = "hello";
char *p = str;
while (*p != '\0') {
    *p = *p - 32;
    p++;
}
printf("%s", str);
  1. A. hello (unchanged)
  2. B. HELLO (all uppercase)
  3. C. Undefined behaviour: modifying a string literal
  4. D. Compile-time error

Correct answer: B. HELLO (all uppercase)

char str[] = "hello"; creates a genuine, modifiable local array initialised with the string's contents (unlike char *str = "hello"; which points at a read-only string literal). Subtracting 32 from each lowercase ASCII letter converts it to uppercase; the loop walks and modifies every character until the null terminator, turning str into HELLO.

More C Programming questions

Exams that ask this topic