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

What does "||" mean in C language?

王林
王林Original
2020-07-11 11:57:4142097browse

In C language, "||" represents the logical operator (or), and its operand is of Boolean type, that is, there are only two values ​​​​"0" (representing false) and "1" (representing true). The C language stipulates that in logical operations, all non-zero values ​​are treated as 1.

What does

In C language, "||" represents the logical operator (or), and its operand is of Boolean type, that is, only "0" (meaning false) and "1" (meaning true). The C language stipulates that in logical operations, all non-zero values ​​are treated as 1.

(Recommended tutorial: C Language Tutorial)

The similar operator is "|", which is a bit operator (or), and its operands are bit sequence. The bit sequence can be character type, integer type, long or short integer type, etc. (usually unsigned integer type is selected). In bit operations, logical operations are performed between corresponding bits. Therefore, logically speaking, the bit operation process contains multiple logical operation processes.

Below we use an example to understand the difference between the two.

#include <stdio.h>
int main (void)
{
    unsigned int x = 0x1101;
    unsigned int y = 0x1100;
    /*逻辑操作*/
    printf("sizeof(x || y): %d\n", sizeof(x || y));
    if(x||y)
    {
        printf("x || y : %d(True) \n", x||y);
    }
    else
    {
        printf("x || y : %d(False) \n", x||y);
    }
    /*位操作*/
    printf("sizeof(x | y): %d\n", sizeof(x | y));
    printf("x | y : %x \n", x|y);
    return 0;
}

Because neither variable x nor variable y is 0, executing the statement if(x||y) returns 1. When executing x|y (that is, 1101|1100), the corresponding bits are logically ORed one by one, so the result is 1101.

The program running result is:

sizeof(x || y): 4
x || y : 1(True)
sizeof(x | y): 4
x | y : 1101

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