Home »
C programming
What is the difference between & and && in C?
Here, operator & is Bitwise AND and Address of Operator, while && is Logical AND Operator.
& as "Address of" Operator
Operator & is a Unary Address Of Operator which returns address of a variable. Basically & is used two times when we are storing values in variable and print the address of any variable.
Consider the following code snippet
scanf("%d",&num);
printf("Address of num: %8X\n",&num);
In the first statement integer value will be stored in num because &num pointing the address of variable num.
In the second statement address of num will print.
& as "Bitwise AND" Operator
Bitwise AND Operator (&) is Binary Operator because it operates on two operands and used for Bitwise AND operation.
Consider the following code snippet
int x=3;
if(x&1)
printf("ONE");
if(x&2)
printf("TWO");
In this statement value of x is 3, so both bits are 1, condition (x&1) and (x&2) will be true and "ONETWO" both messages will print.
Logical AND Operator (&&)
Logical AND (&&) is a Binary Operator which is used to check more than one conditions at a time and if all conditions are true result will be true.
Consider the following code snippet
int x=10;
int y=20;
if(x==10 && y==20)
printf("TRUE");
else
printf("FALSE");
In this statement both conditions x==10 and y==20 are true, hence "TRUE" will print.