3 ms·
I think the C example is meaningfully different. \0 in C is a string terminator. There is no more string after it, so you can just read the string "while (c !=
by qsort 2y ago
I think the C example is meaningfully different. \0 in C is a string terminator. There is no more string after it, so you can just read the string "while (c != 0)", and there is never any need to escape it, because by definition strings cannot contain that character.
From a purely linguistic standpoint, the choice is sound. Obviously it's objectively a disaster but for unrelated reasons.
- acqq 2y ago> the C example ... by definition strings cannot contain that character They can, only some functions stop at the first zero: char s[] = "gogo\x00gogo"; printf( "%d\n", (int)sizeof( s ) );
- qsort 2y agosizeof s (fun fact: you can omit the parentheses because it's not a function, it's an operator) reports the size of the array s, which is a variable of automatic storage duration. Consider the following program: #include <stdio.h> #include <string.h> int main() { const char * const s = "gogo\x00gogo"; const char t[] = "gogo\x00gogo"; printf("%d,%d,%d,%d\n", (int)sizeof s, (int)strlen(s), // DO NOT EVER USE THIS FUNCTION // YOU WILL BE FIRED IF YOU DO (int)sizeof t, (int)strlen(t) // DO NOT EVER USE THIS FUNCTION // YOU WILL BE FIRED IF YOU DO ); } > 8,4,10,4
- lelanthran 2y ago> > the C example ... by definition strings cannot contain that character > They can, only some functions stop at the first zero: > char s[] = "gogo\x00gogo"; And in what human language is that `\x00` a valid character? What rune/glyph/symbol represents it? Nothing, you say? Well, looks like a good choice to me.