80 80 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? $12$ $120400$ $1204$ $1034$ Programming in C gatecse-2015-set3 programming programming-in-c normal array + – go_editor 30.1k views answer comment Share Follow Print See all 16 Comments 16 16 Comments reply Show 13 previous comments Avani_Satam commented Jun 26 reply Follow flag Excellent Answer 🙌🏽 0 0 replyShare satyag33 commented Sep 11 reply Follow flag This is why , I love gate overflow , people don't just answer for the questions asked , they think beyond original answers & they express their own perspectives , so that we can gain more knowledge . Thank you so much for this great community.Don't miss the answers of @tusharp and @Kshitij Sharma's answers in comments and @Ahwan's answer under the best answer. 1 1 replyShare Himanshu_Sharma 16 commented Sep 22 reply Follow flag this has to be the best explanation 0 0 replyShare Please log in or register to add a comment.
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. Arjun answered Feb 14, 2015 • edited Oct 6, 2019 by Arjun Arjun comment Share Follow See all 23 Comments 23 23 Comments reply Show 20 previous comments srestha commented Sep 16, 2019 reply Follow flag https://gateoverflow.in/16618/doubt-regarding-string-constant 1 1 replyShare Rijul commented Jan 12, 2020 reply Follow flag Great Explanation. Helped a lot in cracking the confusion. 0 0 replyShare RRaman commented Oct 21, 2021 reply Follow flag I still didn't get it when we declared the array of characters as s1[7], we wasted the memory because we used only 4 characters and a null at last. so they did it purposefully in the question? 1 1 replyShare Please log in or register to add a comment.
72 72 votes The answer is C.Here*p = '0'; So answer is 1204If *p = 0; Here answer will be 120 means ASCII 0, which is the Null character.'0' means ASCII 48, which is character '0' Ahwan answered Aug 17, 2017 • edited Aug 7, 2025 by Hira Thakur Ahwan comment Share Follow See all 9 Comments 9 9 Comments reply Show 6 previous comments jlimbasiya commented Jun 15, 2019 reply Follow flag *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? 1 1 replyShare Arjun commented Jun 15, 2019 reply Follow flag No. \0 is a valid character as \ is an escape character and it's ASCII code is 0. 6 6 replyShare kp1 commented Oct 6, 2019 reply Follow flag Anything which can be stored in 1Byte is called as char. Computer understands this only. Although we are able to see that 0 is integer. But computer checks whether the thing can be stored in 1 byte or not.. 0 0 replyShare Please log in or register to add a comment.
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. rpdhakad answered Jul 20, 2015 rpdhakad comment Share Follow 0 reply Please log in or register to add a comment.
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 sp_gateoverflow answered Oct 6, 2019 sp_gateoverflow comment Share Follow See 1 comment 1 1 comment reply rishabhmalik commented Jan 19, 2021 reply Follow flag if *p=0 then the output will be 12 only. because 0 acts as Null here and printf will print until NULL character. It should be *p=’0’. 0 0 replyShare Please log in or register to add a comment.
1 1 vote 1204 will be correct answer. DurgaSPandey answered May 5, 2017 DurgaSPandey comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes # include <stdio.h> int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = 48; printf("%s", s1); } output=====1204 because ASCII value of 48 is 0 https://theasciicode.com.ar/ascii-printable-characters/number-one-ascii-code-49.html#:~:text=To%20get%20the%20letter%2C%20character,%221%22%20in%20ASCII%20table. val_pro20 answered Jan 9, 2021 val_pro20 comment Share Follow 0 reply Please log in or register to add a comment.