Question discussion

What is the output of the following C program?#include #include int main() { char *s = "KNOWLEDGE"; char *p = s + strlen(s) - 1; while (p >…

1555Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C program?

#include <stdio.h>
#include <string.h>
int main() {
    char *s = "KNOWLEDGE";
    char *p = s + strlen(s) - 1;
    while (p > s) { putchar(*p); p -= 2; }
    return 0;
}
  • AEDLN
  • BEDLOK
  • CEGWN
  • DEDLO
Correct answer: D

Explanation

p starts at index 8 and moves back by 2, printing E (8), D (6), L (4) and O (2); at index 0 the loop stops because p is no longer greater than s.

Discussion 0

Nobody has replied yet. Start the discussion!