Home  >  Article  >  Backend Development  >  What does "&&" mean in C language?

What does "&&" mean in C language?

青灯夜游
青灯夜游Original
2020-07-23 10:52:48109788browse

In C language, "&&" means "and" and "and". It is a logical operator, which means logical AND; that is, when both conditions are true at the same time, the result of the operation is "true", otherwise it is "false".

What does

In C language && is a logical operator, which represents logical AND; it is equivalent to the "and" in life, which is the situation where both conditions are true at the same time. The result of the "logical AND" operation is "true".

In programming, we generally refer to zero values ​​as "false" and non-zero values ​​as "true". The results of logical operations are only "true" and "false". The corresponding value of "true" is 1 and the corresponding value of "false" is 0.

There are the following three situations:

1. When the left side of the logical sum is false (false), the judgment of the logical sum on the right side is no longer performed, and the result is false (false)

2. When the left side of the logical AND is true (true), the right side is judged. The right side is false (false), and the result is false (false)

3. When the logical AND left side is true (true), the right side is judged. Judgment on the right side, if the right side is also true (true), then the result is true (true)

and operation (&&)

The two expressions involved in the operation are both The result is true when true, otherwise it is false. For example:

5&&0

5 is true, 0 is false, and the result of the AND is false, which is 0.

(5>0) && (4>2)

5>The result of 0 is 1, which is true, and the result of 4>2 is 1, which is also true, so the result of AND is true, which is 1.

Example:

#include <stdio.h>
#include <string.h> // strcmp所在头文件
 
int main(void) 
{
char szAccount[] = "account";    // 设置的默认账号
char szPassword[] = "password";  // 设置的默认密码
char szEntryAccount[32] = { 0 };  // 输入的账号
char szEntryPassword[32] = { 0 }; // 输入的密码
 
// 获取输入的账号
printf("请输入账号: ");
scanf_s("%s", szEntryAccount, 32);
 
// 获取输入的密码
printf("请输入密码: ");
scanf_s("%s", szEntryPassword, 32);
 
// 通过strcmp函数进行比较输入的账号和密码是否跟我们设置的匹配 
// 通过 &&(逻辑与) 进行判断,当账号和密码都正确才会打印出 "账号密码正确!"
if (strcmp(szEntryAccount, szAccount) == 0 &&
    strcmp(szEntryPassword, szPassword) == 0) {
printf("账号密码正确!");
}
else {
printf("账号或密码错误!");
}
 
 
return 0;
}

Related recommendations: "c Language Tutorial"

The above is the detailed content of What does "&&" mean in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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