Has TRUE always had a non-zero value?

前端 未结 22 819
予麋鹿
予麋鹿 2020-12-31 01:38

I have a co-worker that maintains that TRUE used to be defined as 0 and all other values were FALSE. I could swear that every language I\'ve worked with, if you could even

相关标签:
22条回答
  • 2020-12-31 01:41

    I'm not certain, but I can tell you this: tricks relying on the underlying nature of TRUE and FALSE are prone to error because the definition of these values is left up to the implementer of the language (or, at the very least, the specifier).

    0 讨论(0)
  • 2020-12-31 01:41

    I recall doing some VB programming in an access form where True was -1.

    0 讨论(0)
  • 2020-12-31 01:41

    The funny thing is that it depends on the language your are working with. In Lua is true == zero internal for performance.. Same for many syscalls in C.

    0 讨论(0)
  • 2020-12-31 01:43

    For languages without a built in boolean type, the only convention that I have seen is to define TRUE as 1 and FALSE as 0. For example, in C, the if statement will execute the if clause if the conditional expression evaluates to anything other than 0.

    I even once saw a coding guidelines document which specifically said not to redefine TRUE and FALSE. :)

    If you are using a language that has a built in boolean, like C++, then keywords true and false are part of the language, and you should not rely on how they are actually implemented.

    0 讨论(0)
  • 2020-12-31 01:48

    The 0 / non-0 thing your coworker is confused about is probably referring to when people use numeric values as return value indicating success, not truth (i.e. in bash scripts and some styles of C/C++).

    Using 0 = success allows for a much greater precision in specifying causes of failure (e.g. 1 = missing file, 2 = missing limb, and so on).

    As a side note: in Ruby, the only false values are nil and false. 0 is true, but not as opposed to other numbers. 0 is true because it's an instance of the object 0.

    0 讨论(0)
  • 2020-12-31 01:49

    It might be in reference to a result code of 0 which in most cases after a process has run, a result code of 0 meant, "Hey, everything worked fine, no problems here."

    0 讨论(0)
提交回复
热议问题