Explain the concepts of logical operators and assignment operators in C language

王林
Release: 2023-09-13 18:17:13
forward
1037 people have browsed it

Explain the concepts of logical operators and assignment operators in C language

First, let’s learn about logical operators.

Logical Operators

  • These are used to logically combine two (or more) expressions.

  • They are logical AND (&&), logical OR (||) and logical NOT (!)

Logical AND ( &&)

exp1 exp2 exp1&&exp2
T T T
T F F
F T F
F F F

##Logical OR (||)

exp1 exp2 exp1||exp2 ##T T F F
T T
F T
T T
F #F

##Logical NOT (!)

exp !exp T T F T

Operator Description Example a=10,b=20,c=30 Output ##&& Logical AND ##|| Logical OR (a>b)||(a(10>20)||(101 ! logical negation !(a>b) !(10>20) 1 The following is a C program to calculate logical operators:
(a>b)&&(a (10>20)&&(10 0
Example
Demonstration

#include main (){ float a=0.5,b=0.3,c=0.7; printf("%d

",(ac));//0// printf("%d

",(a>=b)&&(b<=c));//1// printf("%d

",(a==b)||(b==c));//0// printf("%d

",(b>=a)||(a==c));//0// printf("%d

",(b<=c)&&!(c>=a));//0// printf("%d

",!(b<=c)||(c>=a));//1// }

Copy after login

Output

You will see the following Output -

0 1 0 0 0 1
Copy after login

Assignment operator

is used to assign a value to a variable.

Type

The type of the assignment operator is -

Simple assignment

Compound assignment

< ul class="list">
  • Operator Description Example Simple assignment a=10 a = 10"a=a 10 below Given is a C program for compound assignment operator - Live Demonstration
    ##=
    =,-=,*=,/=,%= Compound assignment
    a=10"a=a-10

    Procedure

    #include int main(void){ int i; char a='h'; printf("enter the value of i:

    "); scanf("%d",&i); printf("print ASCII value of %c is %d

    ", a, a); a += 5; printf("print ASCII value of %c is %d

    ", a, a); a *= a + i; printf("a = %d

    ", a); a *= 3; printf("a = %d

    ", a); a /= 2; printf("a = %d

    ", a); a %= 4; printf("a = %d

    ", a); return 0; }

    Copy after login
    < h2> Output

    You will see the following output -

    enter the value of i: 3 print ASCII value of h is 104 print ASCII value of m is 109 a = -80 a = 16 a = 8 a = 0
    Copy after login

    The above is the detailed content of Explain the concepts of logical operators and assignment operators in C language. For more information, please follow other related articles on the PHP Chinese website!

    source:tutorialspoint.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!