Consider the following C program segment. # include <stdio.h> int main() {…

2015

Consider the following C program segment.

# include <stdio.h> int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = '0'; printf("%s", s1); }

What will be printed by the program?

  1. A.

    12

  2. B.

    120400

  3. C.

    1204

  4. D.

    1034

Attempted by 181 students.

Show answer & explanation

Correct answer: C

Answer: 1204

Explanation:

  • The array s1 is initialized to the string "1234", which is stored as the characters '1', '2', '3', '4', '\0'.

  • The statement p = s1 + 2 sets p to point to the character at index 2 (the third character) of s1, which is '3'.

  • The assignment *p = '0' overwrites that character with '0', so the stored characters become '1', '2', '0', '4', '\0'.

  • printf("%s", s1) prints the characters up to the null terminator, so the output is 1204.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir