GATE / CS ARCHIVE

GATE CSE 2015 SET 3 / QUESTION 11

GATE CSE 2015 Set 3 Q11

Programming in C · Arrays & Strings · Full GATE CSE 2015 Set 3 paper

GATE 2015 Set 3, Q11 1 mark · MCQ
Report issue

Arrays & Strings

Question 11, GATE 2015: What does this C program print?
#include <stdio.h>
int main() { char s1[7]="1234", *p; p=s1+2; *p='0'; printf("%s",s1); }
The assigned character is digit '0', not the null character '\0'. Options: (A) 12; (B) 120400; (C) 1204; (D) 1034.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (8 steps)

1. Initially the character array begins with 1,2,3,4 followed by a null terminator. Pointer arithmetic advances by characters, so s1+2 selects the third stored character. The assignment changes that position only; printf with %s stops at the unchanged original terminator.

2. p points to s1[2]. Assigning character'0' replaces digit 3, not the terminating null byte. The string becomes 1204 followed by its original terminator, so C.

Original paper, page 57 · Answer key