Home > Backend Development > C++ > Explain the concepts of logical operators and assignment operators in C language

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

王林
Release: 2023-09-13 18:17:13
forward
1165 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 (||)

exp1exp2exp1||exp2##TTFF
T T
F T
T T
F #F

##Logical NOT (!)

exp!expTTFT

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

#include<stdio.h>
main (){
   float a=0.5,b=0.3,c=0.7;
   printf("%d</p><p>",(a<b)&&(b>c));//0//
   printf("%d</p><p>",(a>=b)&&(b<=c));//1//
   printf("%d</p><p>",(a==b)||(b==c));//0//
   printf("%d</p><p>",(b>=a)||(a==c));//0//
   printf("%d</p><p>",(b<=c)&&!(c>=a));//0//
   printf("%d</p><p>",!(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">
  • OperatorDescriptionExampleSimple assignment a=10a = 10"a=a 10below Given is a C program for compound assignment operator - Live Demonstration
    ##=
    =,-=,*=,/=,%=Compound assignment
    a=10"a=a-10

    Procedure

    #include<stdio.h>
    int main(void){
       int i;
       char a=&#39;h&#39;;
       printf("enter the value of i:</p><p>");
       scanf("%d",&i);
       printf("print ASCII value of %c is %d</p><p>", a, a);
       a += 5;
       printf("print ASCII value of %c is %d</p><p>", a, a);
       a *= a + i;
       printf("a = %d</p><p>", a);
       a *= 3;
       printf("a = %d</p><p>", a);
       a /= 2;
       printf("a = %d</p><p>", a);
       a %= 4;
       printf("a = %d</p><p>", 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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template