Not grasping Booleans

This is a discussion on Not grasping Booleans within the C Programming forums, part of the General Programming Boards category; Hi, Im very new to programming and Im liking the way this website is explaining c but I'm having some ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Not grasping Booleans

    Hi,
    Im very new to programming and Im liking the way this website is explaining c but I'm having some trouble understanding booleans.

    An example is listed as follows:
    !( 1 || 0 ) ANSWER: 0

    to me this reads Not 1 or 0
    so if the the value is not 1 or 0, how is the answer 0?

    Additionally in the chapter 2 quiz question 3 reads:
    3. Evaluate !(1 && !(0 || 1)).
    A. True
    B. False
    C. Unevaluatable

    The answer is listed as "A true", but why? Doesnt this statement have to be compared to something in order to be true or false?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Zero or one is true, which in C means non-zero. The negation of that is false which in C means a zero value.

    So, to sum up: anything != 0 evaluates to true
    0 evaluates to false

    Note, that there is an actual bool type defined in stdbool.h which was introduced in C99 I believe, however most old school C programmers don't use it, because "it's not the proper way to write C "
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by esmelogo
    An example is listed as follows:
    !( 1 || 0 ) ANSWER: 0

    to me this reads Not 1 or 0
    so if the the value is not 1 or 0, how is the answer 0?
    The result of (1 || 0) is 1. The result of !1 is 0.

    Quote Originally Posted by esmelogo
    Additionally in the chapter 2 quiz question 3 reads:
    3. Evaluate !(1 && !(0 || 1)).
    A. True
    B. False
    C. Unevaluatable

    The answer is listed as "A true", but why? Doesnt this statement have to be compared to something in order to be true or false?
    No, it is an expression. For example, you do not need to assign the result of 1+1 to a variable in order for the expression 1+1 to evaluate to 2. Likewise, you do not need to compare !(1 && !(0 || 1)) with anything for it to be evaluated to true (or 1).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array of booleans
    By eklavya8 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2008, 02:36 PM
  2. Array of Booleans Represented with 1 Bit
    By Brad0407 in forum C++ Programming
    Replies: 43
    Last Post: 07-08-2007, 01:23 PM
  3. Heaps of booleans
    By Boksha in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2006, 03:44 PM
  4. Help plz, Booleans
    By tagman in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2005, 08:30 AM
  5. Booleans in C
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 09-09-2004, 09:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21