News for hackers. Stuff that matters.
Re:Did Ken, Ritchie and Brian choose wrong with NUL-terminat... by _kst_ on Tuesday January 16, 2018 @03:28PM (#16162128)
Use "%zu" to print a value of type size_t.

A couple of ugly corner cases:

    const char str[] = "abcd\0efgh";
    printf("length = %zu, size = %zu, value = \"%s\"\n",
           strlen(str), sizeof str, str);
output:

    length = 4, size = 10, value = "abcd"
And:

    const char str[4] = "abcd";
    printf("length = %zu, size = %zu, value = \"%s\"\n",
           strlen(str), sizeof str, str);
This has undefined behavior. (Which is a good reason to let the compiler figure out how big the array has to be. Computers are better at counting things than you are. Let them.)

There are no comments on this comment yet.