Logical operator of C language

Source: Internet
Author: User
Tags logical operators true true

A logical operator:

&&: Logic and, Read and

Expressions are true on both sides of the expression, so the result is true.

Formula: a false or False

|| : logical OR, read or

Expression the left and right sides, there is a true, then the result is true

Formula: One true is true

!: Logical not, read as reverse

If the result of an expression is false, it becomes true, and if it is true, it becomes false.

Formula: True change false, false change true

Short-circuit problem of two logical operators

Tips: Not 0 is true, 0 is False

Short-Circuit Condition:

&&: Left If False, the right short circuit (right will not be executed)

|| : If true on left, short to right (not on right)

Tips: Code examples are as follows

#include <stdio.h>

int main (int argc, const char * argv[]) {

int res = 1 && 2;

printf ("res=%d\n", res);

int res = 1 &&-2;

printf ("res=%d\n", res);//1

int res = 1 && 0;

printf ("res=%d\n", res);//0

int num1=10,num2=20;

Or the formula: a true true, so if the left is true, then the right expression will not be executed

int res = num1++ | | num2++;

printf ("res=%d num1=%d num2=%d\n", res,num1,num2);//1 11 20

int num1=0,num2=10;

And the formula: a false is false

int res = num1++ && num2++;

printf ("res=%d num1=%d num2=%d\n", res,num1,num2); 0 1 10

int num1=0,num2=10;

int sum = num1++ + num2; 10

int num = 10;

int res = 0 && num++;

printf ("res=%d num=%d\n", res,num); 0 10

int num1=0,num2=10;

And the formula: a false is false

int res = NUM1 && num2++;

printf ("res=%d num1=%d num2=%d\n", res,num1,num2); 1 1 11

int num1=-3,num2=3,num3=4;

int res = (NUM1 + num2) && (NUM1-NUM3);

printf ("res=%d\n", res);//0

int num1=-3,num2=3,num3=4;

int res = (NUM1 + num2) && num3++;

printf ("res=%d num3=%d\n", res,num3);//0 4

int num1=-3,num2=3,num3=4;

int res = num1++ + num2++ && num3++;

printf ("res=%d num1=%d num2=%d num3=%d\n", res,num1,num2,num3);//0-2 4 4

return 0;

}

Logical operator of C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.