-4

I'm very new to C and I'm going through some example code and I'm not sure what these operators in the if statements are "asking", so to say.

Here is the code:

int main(void){

    int a = 99;

    int b = 0;

    int c = 74;

    if( a || b )
      printf("first\n");

    else
      printf("second\n");

    if( a && c )
      printf("third\n");

    else
      printf("fourth\n");

    if( !a )
      printf("fifth\n");

    else
      printf("sixth\n");

    if( (a && b) || c )
      printf("seventh\n");

    else
      printf("eighth\n");

    if( !c || !b )
      printf("nineth\n");

    else
      printf("tenth\n");
}

I know what the operators mean, I just don't understand what is going on when they're going through the "if" statements. Could someone please explain this to me?

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • If you know what the operators mean, then you know they have a truth value, right? And an `if` statement will check the truth value and, if it's true, execute the following statement or block. (NOTE: A number in C has a truth value of "true" if it's non-zero, and "false" if it's zero.) – lurker Sep 27 '13 at 02:13
  • These are boolean operators . I suggest writing a small application to play with the different expressions and different input values – TGH Sep 27 '13 at 02:14
  • This question appears to be off-topic because the OP has done no basic research –  Sep 27 '13 at 02:14
  • You don't know what they mean if you don't know what they are doing... – Ed S. Sep 27 '13 at 02:15
  • @mbratch Right, but if the statement is if(a || b) what is it asking? All I'm seeing is "if a or b" I don't understand the full extent of it. – Preston May Sep 27 '13 at 02:16
  • possible duplicate of [Logical Operators in C](http://stackoverflow.com/questions/12332316/logical-operators-in-c) – Shafik Yaghmour Sep 27 '13 at 02:17
  • You said you know what the operators mean. What does `||` mean? Then apply that logic to the values of `a` and `b` based upon what I said previously. – lurker Sep 27 '13 at 02:18
  • 1
    @mbratch It means "or", I figured it out now. Thanks. – Preston May Sep 27 '13 at 02:21

3 Answers3

1

The key to answering this question is realization of how C treats integers that participate in logical operations:

  • Zero is treated as FALSE
  • All values other than zero are treated as TRUE

Here are the truth tables for the three operators from your code snippet:

!FALSE -> TRUE
!TRUE  -> FALSE

FALSE || FALSE -> FALSE
FALSE || TRUE  -> TRUE
TRUE  || FALSE -> TRUE
TRUE  || TRUE  -> TRUE

FALSE && FALSE -> FALSE
FALSE && TRUE  -> FALSE
TRUE  && FALSE -> FALSE
TRUE  && TRUE  -> TRUE

When multiple operators are used in an expression without parentheses, unary ! is applied ahead of the binary && or ||.

Now you have enough information to figure out the output yourself.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

They are just normal operators:

|| = or
&& = and
! = not

So...

if( a || b )
  printf("first\n");

Is saying...

if a or b then print "first"

And...

if ( !a )

Is...

if not a

Equivalent to:

if a is equal to 0 or NULL

Really basic stuff!

Craig
  • 4,268
  • 4
  • 36
  • 53
0

It might be easier to learn about boolean operators if you are working with variables of type bool. I find boolean checks to be less useful when dealing with ints since it's less common to think of a number as being true or false. With ints you typically consider their numerical values which makes less sense in boolean logic

TGH
  • 38,769
  • 12
  • 102
  • 135