lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>
> In my opinion, the C/C++ behavior is just a side effect of C-strings
> being terminated by a null byte, aka 0. Since C didn't have a true
> boolean type (correct me if I'm wrong), 0 was used as a false value.
> Hence, an empty string is treated as a false value.
>

I don't think that's the case, whether a string in C is empty (null
terminated) or not doesn't affect the truth value of it.

--
char *s = "hello";
char *t = "";

if (s)
  printf("s is true\n");

if (t)
  printf("t is true\n");

Both will print a message saying they're true, because s and t are
pointers to locations in memory which are both true.  C more likely
deals with truth values as a consequence of how underlying hardware
typically treats 0 as false and everything else as true.  C strings
would've been built on top of that behaviour.