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? $12$ $120400$ $1204$ $1034$ Programming in C gatecse-2015-set3 programming programming-in-c normal array + – go_editor 29.2k views answer comment Share Follow Print See all 15 Comments 15 15 Comments reply Show 12 previous comments Umesh Shelke commented Sep 17, 2025 reply Follow flag @Kshitij Sharma $\text{ Good Explanation . }$ 0 0 replyShare 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 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 kumar_sanjay commented Sep 27, 2016 reply Follow flag what happen if ' a'. Is 12a4 printed? I want how above works 0 0 replyShare ManojK commented Sep 27, 2016 reply Follow flag Yes .Similar working as above. Execute this code # include <stdio.h> int main() { char s1[7] = "1234", *p; p = s1 + 2; *p = 'a'; printf("%s", s1); } 0 0 replyShare priyanka gautam-piya commented Feb 1, 2017 reply Follow flag here why not b?? 0 0 replyShare Arjun commented Feb 1, 2017 reply Follow flag @priyanka There is no string in C language. So, for using a string we start at any address and scan characters from consecutive locations until a '\0' character which is byte value 0 is encountered. This is how all string functions in C works - including "%s" in printf, strlen, strcpy, strcat etc. 18 18 replyShare priyanka gautam-piya commented Feb 1, 2017 reply Follow flag okk means the empty location are consider as null chracter or not like array where we initialise them as 0 1 1 replyShare Arjun commented Feb 1, 2017 i edited by Arjun Feb 2, 2017 reply Follow flag No. There is no empty location in C/C++. All memory locations by default have garbage (static/global variables are initialized though). So, for string ending character we have to put '\0' or equivalently 0 to that location. But when we use string literals like "1234" compiler adds this '\0' at end and makes it 5 bytes which contain "1", "2", "3", "4", "\0" in consecutive memory locations. 28 28 replyShare sanyam53 commented Feb 2, 2017 reply Follow flag "scan characters from consecutive locations until a '\0' character" @arjun sir, in that case ans should be (A)12 because it print upto null char...??? 1 1 replyShare Arjun commented Feb 2, 2017 reply Follow flag @sanyam you are correct. But it was a typo in question. It was '0' and not '\0'; corrected now. 1 1 replyShare srestha commented Sep 2, 2019 reply Follow flag I read, that string is a ROM location of computer memory. It's value cannot be changed. And if u change it's value, it will terminated abnormally. So, why is it not similar to this program too? @Arjun Sir 0 0 replyShare Shaik Masthan commented Sep 5, 2019 reply Follow flag All Strings Not placed in read only memory... Only constant strings are placed in rom 1 1 replyShare srestha commented Sep 5, 2019 reply Follow flag @Shaik Masthan why this string not a constant string?? 0 0 replyShare Gaurav_Singh2 commented Sep 13, 2019 reply Follow flag @srestha These 2 links might help https://www.geeksforgeeks.org/whats-difference-between-char-s-and-char-s-in-c/ https://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s 0 0 replyShare srestha commented Sep 13, 2019 reply Follow flag @Gaurav_Singh2 Sorry, still my doubt not cleared Can u explain , if u got my point. 0 0 replyShare Gaurav_Singh2 commented Sep 14, 2019 reply Follow flag When we write 1. char *s ="hello"; Creates two objects: a read-only array of 6 chars containing the values 'h', 'e', 'l', 'l', 'o', '\0', which has no name and has static storage duration (meaning that it lives for the entire life of the program); and a variable of type pointer-to-char, called s, which is initialized with the location of the first character in that unnamed, read-only array. The "read-only memory" is the text segment in the program. The same place in memory where the instructions are loaded. When you create a char* initialized to a string, the string data is compiled into the text segment and the program initializes the pointer to point into the text segment and our text segment is of type read-only for obvious reasons like security. So, when we try to change it, a Segmentation fault occurs. 2. char s[] = "hello"; Creates one object - a char array of size 6, called s, initialized with the values 'h', 'e', 'l', 'l', 'o', '\0'. Where this array is allocated in memory depends on where the declaration appears. If the declaration is within a function, it is allocated on the stack; if it's outside a function, it will probably be stored within an "initialized data segment" which we can safely alter at run time. Now, in this case character array s1 is created inside the stack section of the memory and p is a pointer to a character. In the second line p is storing the address of the third element of the array. Notice that p is not pointing to the text segment of the program which is read-only and alteration would give segmentation fault, here p is pointing to the stack section so we can safely alter the values that's why when we do *p = '0' ; it is valid. 19 19 replyShare srestha commented Sep 14, 2019 reply Follow flag @Gaurav_Singh2 Good . So, u mean char s1[]="1234" and try to alter 3 as 0, then it will give segmentation fault?? 0 0 replyShare Gaurav_Singh2 commented Sep 14, 2019 reply Follow flag @srestha try visualising these 3 codes Code 1 #include <stdio.h> int main() { char s1[] = "1234"; printf("%s\n", s1); s1[2] = '0'; printf("%s", s1); return 0; } O/P : 1234 1204 Code 2: # include <stdio.h> int main() { char *s1 = "1234"; printf("%s", s1); return 0; } O/P : 1234 Code 3: # include <stdio.h> int main() { char *s1 = "1234"; s1[2] = '0'; printf("%s", s1); return 0; } O/P : Segmentation Fault (SIGSEGV) 3 3 replyShare srestha commented Sep 14, 2019 reply Follow flag but , this(https://ideone.com/Nmgmia) is not seg fault, as u written :( 1 1 replyShare Gaurav_Singh2 commented Sep 14, 2019 reply Follow flag Yes, this is not a segmentation fault because global string s1 would be stored in the initialized read-write area of the initialized data segment and p is the pointer which stores the address of the third element of the array as defined by you. Now, since the pointer p here is pointing to the initialized data segment which is read-write type so doing *p='0' is valid. But earlier in the above comment point 1, s was pointing to the text section and the text section is read-only type. So, when we will try to do *s='0' then it will cause a segmentation fault. 3 3 replyShare srestha commented Sep 14, 2019 reply Follow flag @Gaurav_Singh2 Is that means, if we modify some value, inside a array that initialized a string, is still no error will occur?? But why not?? Is it's value not constant?? 0 0 replyShare srestha commented Sep 14, 2019 reply Follow flag @Gaurav_Singh2 @Shaik Masthan What I mean see below: check this similar kind of program main(){ printf("%s",*("INDIA"+2)='I'); } will give abnormal termination. Why same thing will not happen here?? 0 0 replyShare 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.
70 70 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.