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 (!).
在C编程语言中,有三个逻辑运算符
Logical AND(&&),
Logical OR(||)和
Logician NOT(!)。
C语言中的逻辑AND(&&)运算符 (Logical AND (&&) operator in C)
Logical ANDis 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 (&&) operatorreturns 1 (true), else it returns 0 (false).
如果两个操作数的值都不
为零(true),则
逻辑AND(&&)运算符返回1(true),否则返回0(false)。
Syntax of Logical AND operator: 逻辑AND运算符的语法:condition1 && condition2
Truth table of logical AND operator: 逻辑AND运算符的真值表: condition1 condition2 condition1 && condition2
1 1 1
1 0 0
0 1 0
0 0 0
逻辑AND(&&)运算符的C示例 (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.
示例1:取一个数字并应用一些条件,并打印包含逻辑AND运算符的表达式的结果。
// 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.
示例2:输入年龄并检查年龄是否未满。
// 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.
示例3:输入用户名和密码,并使用用户名和密码的预定义值验证值。
// 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 "[email protected]"
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: [email protected]
Input credentials are correct
Second run:
Enter username: admin
Enter password: [email protected]
Input credentials are not correct
翻译自: https://www.includehelp.com/c/logical-and-operator-with-example-in-c.aspx