Difference Between & and && in C Programming


The "&" and "&&" operators are both logical AND operators in most programming languages, but they can behave differently in certain circumstances. Both "&" and "&&" are operators used for evaluating conditional statements. The most basic difference between the two is that the "&" operator is a logical as well as a bitwise operator, whereas the "&&" operator is only a logical operator.

Read this article to find out more about these two operators and how they are different from each other. Let's start with a basic overview of "&" and "&&" operators.

What is "&" Operator?

The & operator is a logical operator as well as a bitwise operator. Therefore, the "&" operator works on Boolean as well as binary data (bits). When the "&" operator is used as a logical operator, then it produces result as TRUE or FALSE. It gives a result TRUE if both sides of the conditional statement are true, otherwise it produces a FALSE result.

The "&" operator allows the compiler to evaluate both sides of the conditional expression. Consequently, it evaluates the right side of the expression, even if the left-hand side of the expression results FALSE.

The "&" operator is a bitwise AND operator, which operates on the individual bits of its operands. It compares each bit of the first operand to the corresponding bit of the second operand, and if both bits are 1, it sets the corresponding result bit to 1. Otherwise, it sets the result bit to 0.

What is "&&" Operator?

The && operator is purely a logical operator. Therefore, the && operator works only on the Boolean data types, i.e., bits. The "&&" operator is also known as short-circuit operator. This is because it only checks the left-hand side of the conditional statement.

The && operator is a logical AND operator, which operates on Boolean expressions. It returns True if both of its operands are True, else it returns False. If the left-hand side of the conditional statement results in FALSE, then it does not evaluate the right-hand side of the conditional expression, because the result is already known to be False.

Difference between "&" and "&&"

The following table highlights all the important differences between "&" and "&&" operators −

S. No.

"&" Operator

"&&" Operator

1.

It is a bitwise operator.

It is a logical operator.

2.

It evaluates the left and right side of the expression.

It evaluates the left side of the expression only.

3.

It operates on the 'Boolean' datatype (True or False).

It operates on 'Boolean' datatype only.

4.

It also operates on bits.

It does not operate on bits.

5.

It is used to check logical conditions.

It is used to check logical conditions.

6.

Example

#include<stdio.h>
int main(){
   int x = 3;
   int y = 4;
   int z = x & y;
   printf ("z = %d", z);
   return 0;
}

Example

#include<stdio.h>
int main(){
   int a = 6, b = 3;
   printf("%d", a&&b);
   return 0;
}

Conclusion

To conclude, the most significant difference between the two is that the "&" operator is a bitwise and logical operator, while the "&&" operator is a purely logical operator.

Updated on: 11-Sep-2023

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements