← Quiz: String Handling in C: all 15 questions
Quiz: String Handling in C · Question 2 of 15
What is wrong with char s = "hello"; s[0] = 'H';?
What is wrong with char *s = "hello"; s[0] = 'H';?
- A.Nothing — it prints Hello
- B.It changes a string literal, which is undefined behaviour and usually crashes
- C.It does not compile in any compiler
- D.s[0] is the '\0' character
Answer & explanation
Correct answer: It changes a string literal, which is undefined behaviour and usually crashes
String literals usually live in read-only memory. Use char s[] = "hello"; to get a copy you can change.
Can you get the rest right without peeking?
Take the timed Quiz: String Handling in C quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →