edited by
29,209 views
77 77 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$

9 Answers

Best answer
95 95 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
70 70 votes

The answer is C.
Here
*p = '0';    So answer is 1204
If *p = 0;   Here answer will be 12

0 means ASCII 0, which is the Null character.

'0'  means ASCII 48, which is character '0'
 

edited by
9 9 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.
2 2 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
1 1 vote
1204 will be correct answer.
Answer:
Position:
Show:

Related questions

75 75 votes
9 answers 9 answers
18.4k
18.4k views
go_editor asked Feb 15, 2015
18,443 views
Consider the following two C code segments. $Y$ and $X$ are one and two dimensional arrays of size $n$ and $ n \times n$ respectively, where $2 \leq n \leq 10$. Assume th...
67 67 votes
5 answers 5 answers
20.5k
20.5k views
go_editor asked Feb 16, 2015
20,465 views
Consider the following C program:#include<stdio.h int f1(void); int f2(void); int f3(void); int x=10; int main() { int x=1; x += f1() + f2 () + f3() + f2(); printf("%d", ...
121 121 votes
2 answers 2 answers
36.0k
36.0k views
go_editor asked Feb 16, 2015
35,974 views
Consider the following C program:#include<stdio.h int main() { int i, j, k = 0; j=2 * 3 / 4 + 2.0 / 5 + 8 / 5; k-= j; for (i=0; i<5; i++) { switch(i+k) { case 1: case 2: ...
93 93 votes
12 answers 12 answers
37.1k
37.1k views
go_editor asked Feb 15, 2015
37,083 views
Consider the following C program#include<stdio.h int main() { static int a[] = {10, 20, 30, 40, 50}; static int *p[] = {a, a+3, a+4, a+1, a+2}; int ptr = p; ptr++; print...