Logical AND (&&) operator with example in C language

C language Logical AND (&&) operator: Here, we are going to learn about the Logical AND (&&) operator in C language with its syntax, example.
Submitted by IncludeHelp, on April 14, 2019

Logical operators work with the test conditions and return the result based on the condition's results, these can also be used to validate multiple conditions together.

In C programming language, there are three logical operators Logical AND (&&), Logical OR (||) and Logician NOT (!).

Logical AND (&&) operator in C

Logical AND is denoted by double ampersand characters (&&), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.

If both of the operand's values is non-zero (true), Logical AND (&&) operator returns 1 (true), else it returns 0 (false).

Syntax of Logical AND operator:

    condition1 && condition2

Truth table of logical AND operator:

condition1 	condition2	condition1 && condition2
1		1		1
1		0		0
0		1		0
0		0		0

C examples of Logical AND (&&) operator

Example 1: Take a number and apply some of the conditions and print the result of expression containing logical AND operator.

// C program to demonstrate example of 
// Logical AND (&&) operator 

#include <stdio.h>

int main()
{
    int num =10;
    
    //printing result with AND (&&) operator
    printf("%d\n",(num==10 && num>=5));
    printf("%d\n",(num>=5  && num<=50));
    printf("%d\n",(num!=10 && num>=5));
    printf("%d\n",(num>=20 && num<=50));
    
    return 0;
}

Output

1
1
0
0

Example 2: Input age and check it is teenage or not.

// C program to demonstrate example of 
// Logical AND (&&) operator 
// Input age and check it is teenage or not.

#include <stdio.h>

int main()
{
    int age;
    
    //input age
    printf("Enter age: ");
    scanf("%d", &age);
    
    //check teenage or not
    if(age>=13 && age<=19)
        printf("%d is a teenage value\n", age);
    else
        printf("%d is not a teenage value\n", age);
    
    return 0;
}

Output

First run:
Enter age: 18
18 is a teenage value

Second run:
Enter age: 12
12 is not a teenage value

Example 3: Input username and password and validate the values with predefined values of username and password.

// C program to demonstrate example of 
// Logical AND (&&) operator 
// Input username and password and validate the values 
// with predefined values of username and password.

#include <stdio.h>
#include <string.h>

// define a fixed value for USERNAME and PASSWORD
#define USERNAME "admin"
#define PASSWORD "admin@123"

int main()
{
    char un[50], pass[50];
    
    //input USERNAME and PASSWORD
    printf("Enter username: ");
    scanf("%s", un);
    printf("Enter password: ");
    scanf("%s", pass);
    
    //validate username and password
    if(strcmp(USERNAME,un)==0 && strcmp(PASSWORD, pass)==0)
        printf("Input credentials are correct\n");
    else
        printf("Input credentials are not correct\n");
    
    return 0;
}

Output

First run:
Enter username: admin
Enter password: admin@123
Input credentials are correct

Second run:
Enter username: admin
Enter password: hello@123
Input credentials are not correct




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.