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?
in simple terms initially p was pointing to 1 later it was pointing to 3 and then was asked to change 3 → 0 via this line *p = '0'; after this modification, when you print s1, it will display "1204".
s1
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.
https://gateoverflow.in/16618/doubt-regarding-string-constant
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'
*p = '\0' will also print 12
this assumption is wrong because there is nothing like character '\0' in and may lead to error as we assigning 2 char in one location.
Am I right @Arjun Sir?
64.3k questions
77.9k answers
244k comments
80.0k users