# 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

int main()
{
char un[50], pass[50];

scanf("%s", un);
scanf("%s", pass);

printf("Input credentials are correct\n");
else
printf("Input credentials are not correct\n");

return 0;
}
```

Output

```First run:
Input credentials are correct

Second run:
Input credentials are not correct
```