Operators in C language (Logical and more)

operators in c programming

In C language an operator is a symbol that specifies to be performed the operand. The items that operators act upon are called operands. Some operators require two operands called binary operators, while other acts upon only one operand called unary operator.

Logical operators

Logical operators are used to combine the results of two or more conditions. C language has the following statements and logical operators.

OperatorMeaningExampleReturn value
&&Logical AND(7>2 && (15 >9)1
||Logical OR(9>2) || (15 == 8)1
!Logical NOT29 != 290

&&(and): This operator is usually used in situations, where a set of statements are to be executed in two or more expressions that are true.

Example: (exp1) && (exp2)

||(or): This is used in situations, if any one of them (at least one) is true from two more conditions, then the set of statements is executed.

Example: (exp1) || (exp2)

!(Not): This operator reverses the value of the expression it operates on. It makes a true expression false and a false expression true.

Example:  !(exp1)

‘i’ is an integer variable, whose value is 8

‘f’ is a float variable, whose value is 6.5

‘c’ is a character variable, that represents a character ‘w’.

(i >= 6) && (c == ‘w’)True1
(f < 11) && (i > 100)False0
(c != ‘p’) || (i <= 100)False1

Example program

Program to demonstrate logical operator


Values of c1=45, c2=32, c3=98: 
c1 is greater than c2
c1 is less than c2 or c3 or both

Assignment operator in c language

Assignment operators are used to assigning a value or an expression or a value of a variable to another variable.

Syntax: variable = expression (or) value ;

Description: Variable is any valid ‘c’ variable, assigned value can be anything.

Example: x = 10;

                 x = a + b;

                 x = y;

Example program

Program to demonstrate assignment operator


 k =6

Explanation: This program prints the value of a variable that it contains but that variable in terms assigned by two assigned variables.

Such as k = (i = 4, j =5) means where first ‘i’ value is assigned to ‘k’, then ‘j’ value assigned and replaced the value of ‘i’, so the assigned value only contains the variable ‘k’.

Compound assignment

Apart from assignment operator(=), C language provides compound assignment operators to assign a value to a variable in order to assign a new value to a variable after performing a specified operation.

Some of the Compound Assignment operators and their meanings are given in the table.

+=x += yx = x + y
-=x -= yx = x – y
*=x *= yx = x*y
/=x /= yx = x/y
%=x %= yx = x%y

Nested (or) Multiple Assignments

C programming language has got distinct features in assignments called nested or multiple assignments. Using this feature, we can assign a single value or an expression to multiple variables.

Syntax: var1 = var2 = var3=….varn = single variable or expression;

Description: var1, var2, var3,…varn are called ‘C’ variables.

Example: i=j=k=1;


Increment and Decrement operators (Unary Operation)

C language has two-way useful operators not generally found in other languages. these are the increment (++) and decrement (- -) operators.

The ‘++’ adds one to the variables and ‘- -‘ subtracts one from the variable. These operators are called unary operators. Because they act upon only one variable.

Operators in c language
Operators in c language
Operators in c language
Operators in c language
Operators in c language
operator in c language
Operators in c language

Example program

Program using Increment and Decrement operators.



 a++ = 10
 ++a = 12
 --a = 11
 a-- = 11 

Explanation: In this program, the variable ‘a’ is initialized with the value 10. The post-increment ‘a++‘ produced the output of 10. The pre-increment variable ‘++a‘ produced the output of 12, because ‘a’ increased by 1 at two times.

then pre-decrement variable ‘–a‘ produced the output 11 and variable ‘a–‘ produced the output 11 respectively.

[Note: Do not use increment and decrement on floating-point variables.]

Ternary Operator or Conditional Operator in c language

C language’s conditional or ternary operator itself checks the condition and executes the statements depending on the condition.

Syntax: condition?exp1:exp2;

Description: The ‘?:‘ operator acts as a ternary operator, it first evaluates the condition, if it is true then the ‘exp1’ is evaluated, if the condition is false then the ‘exp2’ is evaluated.


   int a = 7, b = 2, big;
   big = a>b?a:b;
   printf("Big is ...%d",big)


 big is ... 7

In this case, it checks the condition ‘a>b’ if it is true, then the value of ‘a’ is stored in ‘big’ otherwise the value of ‘b’ is stored in ‘big’.

Operators in c language

Bitwise Operators in c language

Bitwise operators are used to manipulating the data at the bit level. It operates on integers only. It may not be applied to float or real. The operators and their meanings are given below.

Bitwise AND (&): This operator is represented as ‘&’ and operates on two operands of integer type.

While operating upon these two operands they are compared on a bit-by-bit basis. (Both the operands must be of the same type i.e int).

&Bitwise AND
|Bitwise OR
^Bitwise XOR
<<Shift left
>>Shift right
~One’s compliment
Operators in c language

Example: x = 7 = 0000 1111

                  y = 8 = 0000 1000

                  x & y = 0000 0000

Bitwise OR (|): Similar to AND operator in all aspects, the truth table for Bitwise OR is shown below.

But this operator gives if either of the operand bit is ‘1’ then the result is ‘1’ or both operands are 1’s then also given ‘1’.

Operators in c language

 Example: x = 7 = 0000 0111

                   y = 8 = 0000 1000

                   x & y = 0000 1111

Bitwise Exclusive OR (^): Similar to AND operator on all aspects but integer gives if either of the operand bit is high(1) then it gives high (1) result. If both operand bits are the same then it gives a low(0) result.

Operators in c language

Example: x = 13 = 0000 1101

                  y = 8   = 0000 1000

                  x ^ Y   = 0000 0101

The special Operator in c language

Along with the operators, the C language supports some of the special operators given below.

,comma operators
sizeofsize of operators
& and *pointer operators
. and –>Member selection operators

The pointer and member selection operators are discussed in the concept of the pointer.

⊕ The comma operator(,):

Usually, the comma operator is used to separate the statement elements such as variables, constants, expressions, etc, and this operator is used to link the related expressions together.

Such expressions can be evaluated from left to right and the value of right most expressions is the value of a combined expression.

Example: val = (a =3, b =9, c =7, a + c);

where First assigns the value 3 to a

Then assigns the value 9 to b

Then assigns the value 77 to c

Finally assigns 80 to the val.

⊕  The sizeof operator:

The sizeof() is a unary operator, that returns the length in bytes of the specified variable, and it is very useful to find the bytes occupied by the specified variable in the memory. The sizeof() operator is a compile-time operator.

⊕  Pointer operators :

&This symbol specifies the address of the variable.
 *This symbol specifies the value of the variable.

⊕  member selection operators. and – -> : These symbols are used to access the elements from a structure.