What will be the output of following program? #include <stdio.h> int main() {…

What will be the output of following program?

#include <stdio.h>

int main() {
   char s1[7] = "abcd";
   char *p;
   
   p = s1 + 2;  
   *p = 'e';    

   printf("%s", s1);
   return 0;
}

Answer: C. abedAnswer: C) abed Explanation: Initial String: s1 stores the string "abcd". Pointer Setup: p = s1 + 2; makes the pointer p point directly to the third character…

  1. A.

    abcde

  2. B.

    abd

  3. C.

    abed

  4. D.

    abcd0e

Attempted by 178 students.

Show answer & explanation

Correct answer: C

Answer: C) abed

Explanation:

  1. Initial String: s1 stores the string "abcd".

  2. Pointer Setup: p = s1 + 2; makes the pointer p point directly to the third character in the string, which is 'c' (at index 2).

  3. Modification: *p = 'e'; modifies the value at that exact memory location, overwriting 'c' with 'e'.

Consequently, the string changes from "abcd" to abed.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…