edited by
15,859 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

Best answer
65 votes
65 votes
p = s1 + 2;

Type of s1 is char[7] and sizeof *s1 is sizeof (char) = 1. So, s1 + 2 will return address in s1 + 2 *sizeof(char) = address in s1 + 2. So,  $p$ now points to the third element in s1.

*p = '0';

The third element in s1 is made $0$. So, $1234$ becomes $1204$. C choice. 

edited by
43 votes
43 votes

Answer is C.
For the confused people,   
*p = '0';    So answer is 1204
If *p = 0;   Here answer will be 12

0 means Ascii 0  which is Null character.

'0'  means Ascii 48 which is character '0'
 

edited by
8 votes
8 votes
Here s1 is an array, So s1 points to base address.

so , p=s1+2 will point to 3rd element of s1.

and *p='0'

value at p (Which is the third element of s1) , so 1234 becomes 1204.
1 votes
1 votes
1204 will be correct answer.
Answer:

Related questions

62 votes
62 votes
11 answers
4