ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
#include <string.h>
int main(void) {
    char s[] = "hello";
    char *p = strchr(s, 'l');
    printf("%ld", (long)(p - s));
}
  1. A. 3
  2. B. 1
  3. C. 4
  4. D. 2

Correct answer: D. 2

strchr returns a pointer to the first l, which is at index 2, so the pointer difference is 2.

More C Programming questions

Exams that ask this topic