edited by
15,872 views
47 votes
47 votes

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. $12$
  2. $120400$
  3. $1204$
  4. $1034$
edited by

8 Answers

1 votes
1 votes
s1[7]="1234"

p=s1+2

it is pointing to the address of "3" of "1234"

if *p=0 then the value of 3 replaced by 0

so the printf("%s", s1) prints 1204
0 votes
0 votes

Here it should have given char *s1, but still the s1[7]=1234 can be answered as s1 meaning the element 1 in the array which is 2 and then 2 is added with s1 so it goes to 4 th element in the array. So *s1=sizeof(char)=1.  s1+2*sizeof(char)= address in s1+2 so now we have to replace the 3rd element with 0 as *p is now pointing to the 3rd element in the array   So 1234 is replaced by 1204 Hence C i the ans.

0 votes
0 votes
*p = s1+2 refers to 3rd element in the array which is 3 and we are changing 3 to 0 ,  

So when we run printf("%s",s1) it will print the entire array s1 .

Now when we run printf("%s",p) it will print 04 because the p pointer is pointing to the 3rd element and it will print from there to the last element .
Answer:

Related questions

62 votes
62 votes
11 answers
4