Interview Question

Interview Mountain View, CA

What will this program do ? ======== int main(void) { char

  *s = "123"; s[0] = 'a'; printf("%s", s); } ========

Interview Answer

4 Answers


It will print a23 if you are lucky. Otherwise it will core dump since "123" is a constant (kept in the text area of your program I believe) and its contents should not be modified.

Interview Candidate on Mar 17, 2011

It will not crash because it is not a constant. You can consider it as a pointer pointing to a modifiable 4-bytes char array.

antliu on Mar 23, 2011

"123" is not a modifiable 4-byte array. It IS a constant and should not be modified. Try this out on different platforms and see what it does.

rumberobueno on Mar 23, 2011

C99 has a const qualifier to define constants. It makes no claims about whether the mentioned array is a non-modifiable array. If your platform crashes and burns on modifying the array then your platforms "c" implementation has an error.

coder on Jun 3, 2011

Add Answers or Comments

To comment on this, Sign In or Sign Up.