48 votes 48 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 asked Feb 14, 2015 • edited Jan 23, 2018 by kenzou go_editor 16.3k views answer comment Share Follow See all 4 Comments See all 4 4 Comments reply tusharp commented Apr 7, 2019 reply Follow Share One more key learning here. If it would have been char *s= "1234", then it will be stored as a string constant in the data segment(read-only). we cannot change the content of string using another pointer. It will give an error. 32 votes 32 votes Chirag Shilwant commented Oct 19, 2019 reply Follow Share 1204 is answer. 0 votes 0 votes kickb commented Dec 14, 2023 reply Follow Share 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". 0 votes 0 votes ritiksri8 commented Mar 25 reply Follow Share Whenever printf sees a null is encountered then it assumes that the string has ended. Answer: 1204 For the confused people, *p = '0'; So answer is 1204 If *p = 0; Here answer will be 12 0 votes 0 votes Please log in or register to add a comment.
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. Arjun answered Feb 14, 2015 • edited Oct 6, 2019 by Arjun Arjun comment Share Follow See all 23 Comments See all 23 23 Comments reply kumar_sanjay commented Sep 27, 2016 reply Follow Share what happen if ' a'. Is 12a4 printed? I want how above works 0 votes 0 votes ManojK commented Sep 27, 2016 reply Follow Share 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 votes 0 votes priyanka gautam-piya commented Feb 1, 2017 reply Follow Share here why not b?? 0 votes 0 votes Arjun commented Feb 1, 2017 reply Follow Share @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. 13 votes 13 votes priyanka gautam-piya commented Feb 1, 2017 reply Follow Share okk means the empty location are consider as null chracter or not like array where we initialise them as 0 0 votes 0 votes Arjun commented Feb 1, 2017 i edited by Arjun Feb 2, 2017 reply Follow Share 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. 25 votes 25 votes sanyam53 commented Feb 2, 2017 reply Follow Share "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 votes 1 votes Arjun commented Feb 2, 2017 reply Follow Share @sanyam you are correct. But it was a typo in question. It was '0' and not '\0'; corrected now. 1 votes 1 votes srestha commented Sep 2, 2019 reply Follow Share 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 votes 0 votes Shaik Masthan commented Sep 5, 2019 reply Follow Share All Strings Not placed in read only memory... Only constant strings are placed in rom 0 votes 0 votes srestha commented Sep 5, 2019 reply Follow Share @Shaik Masthan why this string not a constant string?? 0 votes 0 votes Gaurav_Singh2 commented Sep 13, 2019 reply Follow Share @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 votes 0 votes srestha commented Sep 13, 2019 reply Follow Share @Gaurav_Singh2 Sorry, still my doubt not cleared Can u explain , if u got my point. 0 votes 0 votes Gaurav_Singh2 commented Sep 14, 2019 reply Follow Share 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. 17 votes 17 votes srestha commented Sep 14, 2019 reply Follow Share @Gaurav_Singh2 Good . So, u mean char s1[]="1234" and try to alter 3 as 0, then it will give segmentation fault?? 0 votes 0 votes Gaurav_Singh2 commented Sep 14, 2019 reply Follow Share @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 votes 3 votes srestha commented Sep 14, 2019 reply Follow Share but , this(https://ideone.com/Nmgmia) is not seg fault, as u written :( 1 votes 1 votes Gaurav_Singh2 commented Sep 14, 2019 reply Follow Share 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 votes 3 votes srestha commented Sep 14, 2019 reply Follow Share @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 votes 0 votes srestha commented Sep 14, 2019 reply Follow Share @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 votes 0 votes srestha commented Sep 16, 2019 reply Follow Share https://gateoverflow.in/16618/doubt-regarding-string-constant 1 votes 1 votes Rijul commented Jan 12, 2020 reply Follow Share Great Explanation. Helped a lot in cracking the confusion. 0 votes 0 votes RRaman commented Oct 21, 2021 reply Follow Share 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? 0 votes 0 votes Please log in or register to add a comment.
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' Ahwan answered Aug 17, 2017 • edited Aug 23, 2018 by Ahwan Ahwan comment Share Follow See all 9 Comments See all 9 9 Comments reply Show 6 previous comments jlimbasiya commented Jun 15, 2019 reply Follow Share *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 votes 1 votes Arjun commented Jun 15, 2019 reply Follow Share No. \0 is a valid character as \ is an escape character and it's ASCII code is 0. 4 votes 4 votes kp1 commented Oct 6, 2019 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.
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. rpdhakad answered Jul 20, 2015 rpdhakad comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes 1204 will be correct answer. DurgaSPandey answered May 5, 2017 DurgaSPandey comment Share Follow See all 0 reply Please log in or register to add a comment.