How can '\0' end a string ? Logically isn't it incorrect ? For C & C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

How can '\0' end a string ? Logically isn't it incorrect ? For C & C++

We all know a string is an array of characters i.e. every block of array can contain only & only 1 character (as size of char is 1). So how is this possible that computer can write '\0' to end a string, which are 2 characters -> '\' & '0'...

14th Nov 2018, 4:23 AM
Amz42
Amz42 - avatar
4 Answers
+ 9
Also, one of the billion dollars mistakes of the history of language design was null terminator! "The best candidate I have been able to come up with is the C/Unix/Posix use of NUL-terminated text strings. The choice was really simple: Should the C language represent strings as an address + length tuple or just as the address with a magic character (NUL) marking the end? This is a decision that the dynamic trio of Ken Thompson, Dennis Ritchie, and Brian Kernighan must have made one day in the early 1970s, and they had full freedom to choose either way. I have not found any record of the decision, which I admit is a weak point in its candidacy: I do not have proof that it was a conscious decision.[...] ¹" ____ ¹ https://queue.acm.org/detail.cfm?id=2010365
14th Nov 2018, 6:43 AM
Babak
Babak - avatar
+ 5
Too much information! 8D Thanks Kirk for sharing that. I like the fact that they thought it through and then decided to " make it more general and simpler to describe". They were/are humans after all and it's kinda ridiculous to blame them "why they couldn't predict all possible scenario for which their decision would be costly".
14th Nov 2018, 8:24 PM
Babak
Babak - avatar
+ 4
[meta] A quote here (the accepted answer, "from the horse's mouth") on the rationale leading to null-terminated strings: https://stackoverflow.com/questions/4418708/whats-the-rationale-for-null-terminated-strings The article it's quoting (for-pay elsewhere, authorized by Dennis Ritchie to be published here): The Development of the C Language https://www.bell-labs.com/usr/dmr/www/chist.html
14th Nov 2018, 7:03 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
Kirk Schafer nice interesting article Aman Gupta maybe this will address your question in the formula: https://stackoverflow.com/questions/14183445/what-does-0-mean At first when I saw your question I was thinking EOF or a switch "0 000 0 000" as in binary or ASCII as "NUL" https://code.sololearn.com/c9Sr2TJsMcs9/?ref=app
25th Nov 2018, 7:49 PM
BroFar
BroFar - avatar