What is the difference between & and && in C?

Difference between & and &&

What is the difference between & and && in C? - In this section, we will explain difference between & and && operators in C programming language. 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.







Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.