Operators in C

Operators in C, Types of operators in c programming and uses

C operators are used to manipulate data and variables in programming. In simple words C operators are used to perform mathematical or logical operations on variables or data.

  • The most popular operators in c programming are +, -, *, /, =, &, <, >, etc.
  • Each operator has their own unique symbol and functionality.
  • C operators are classified into a number of categories which are listed below.
Types of Operators in C
S.N Operator Type
1 Arithmetic Operators
2 Relational Operators
3 Logical Operators
4 Assignment Operators
5 Increment and decrement operators
6 Conditional Operators
7 Bitwise Operators
8 Special Operators

Arithmetic Operators in C

C Language supports 5 basic arithmetic operators. The arithmetic operators +,-,*,/ and % are supported by C and they behave same as their description. These arithmetic operators can operate on any built-in data types allowed in C.

Arithmetic Operators in C
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division

Output

Explanation

  • The Arithmetic operators +, -, * and / performs operation according to their specifications.
  • Modulo operator % gives remainder for example 13%3 = 1
  • When we divide 13 by 3 then the result should be 13/3 = 4.333, but the result is 4 because we have mared res variable as int type so it will store only complete part and ignore fractional part which is .333 in this candition.
  • If we want to store complete result then we can mark res variable as float data type.

Relational Operators in C

Relational Operators are also called comparison operators. Relational Operators are used to compare two quantities/variables.

  • If result of comparison is true then it returns 1 and if result of comparison is false then it returns 0.
  • Relational Operators are basically used in decision making and looping conditions.
  • There are 6 relational operators in C, which are listed in below table
Relational Operators in C
== Equal to 8==7 returns 0
> Greater than 8>7 returns 1
< Less than 8<7 returns 0
>= Greater than or equal to 8>=7 returns 1
<= Less than or equal to 8<=7 returns 0
!= Not equal to 8!=7 returns 1

Output

* Don't worry if you don't know about if, else conditions, we will learn it in upcoming chapters

Logical Operators in C

Logical Operators are next additional part of relational operators, logical operators are also used for decision making.

  • There are 3 logical operators in C programming language.
  • Logical expression returns 1 if condition is true and returns 0 if condition is false.
Operator Meaning of Operator Example
&& Logical AND, True only if all conditions are true if m=9 and n=3 then, expression ((m>n)&&(n<m)) will be equal to 1
|| Logical OR, True if at least one condition is true if m=9 and n=3 then, expression ((m>n)&&(m<n)) will be equal to 1
! Logical NOT, True only if expression result is 0 If m=9 then, expression !(m==9) will be equal to 0

Output

Explanation

  • In the program, the expression (a<b) && (b>a) is equal to 1, because (a<b) is True and (b>a) is also true. Since these two expression are associated with logical operator &&, and it gives output as 1 if all conditions are true.
  • In the program, the expression (a>b) || (b<a) is equal to 0, because (a>b) is False and (b<a) is also False. Since these two expression are associated with logical operator ||, and it gives output as 0 if all conditions are False.
  • In thired expression !(a==16) is equal to 0 because (a==16) is True and ! is converted it in False and False is treated as 0

Assignment Operators in C

Assignment operators are used to assign the result of an expression to a variable. In C programming = is used as an assignment operator.

In C programming there are some extended forms of assignment operators. Let’s understand by example.

Operator Expression Same As
= m+n n+m
+= m+=n m=m+n
-= m-=n m=m-n
*= m*=n m=m*n
/= m/=n m=m/n
%= m%=n m=m%n

Output

Increment and Decrement Operators

C has two very useful operators i.e increment (++) and decrement (--) operators.

  • ++ operator is used for increment, it increases the value of the variable by 1
  • -- operator is used for decrement, it decreases the value of the variable by 1
  • These operators are always used with integral variables.
  • If a is an int type variable then increment can be done by ++a or a++, both are the same.
  • If b is an int type variable then decrement can be done by --b or b--, both are the same.
  • a++ or ++a means a=a+1
  • b-- or --b means b=b-1

Conditional Operators or Ternary Operator

Conditional Operators in C is also known as ternary operator and represented as the pair of ? :

Syntax of Ternary Operator

In the conditional operator, the condition statement decides which expression will be executed. There can be two scenarios!

  • If the condition is true
  • If the condition is false

A. If the condition is true

If the condition statement is true then expression1 will be executed and the value of the whole expression will be the value of expression1.

Output

B. If the condition is false

If the condition statement is false then expression2 will be executed and the value of the whole expression will be the value of expression2.

Output

Bitwise Operators in C

Bitwise Operators are used for manipulation of data at bit level. There are many four bitwise operators which are listed below are used for testing the bits or shifting them left or right.

Note : Bitwise operators cannot be applied to float or double data types.

Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right

We will learn all about bitwise operator in details in upcoming topics.

Special Operators

C programming offers several special operators which makes C programming more attractive, the some important special operators are comma operator, sizeof operator, pointer operators ( * and & ) and member selection operators ( . and -> )

Operator Meaning
, comma operator
sizeof size of operator
* and & pointer operator
. and -> member selection operator

Comma Operator

Comma operator is used to link several related expression together. See below example how we have implemented related expression together.

int a,b, c=10,d;

In above example we have declared four variables and they are similar types i.e. int type. We have declared all these variables together separated by comma operator (,) instead of typing in four lines.

sozeof Operator

The sizeof operator is used to determine the number of bytes occupied by the opened in run time. In general the opened are variables, constant or data type quantifier. See below examples.

Note : You will learn pointer operators (* and & ) and member selection operator ( . and -> ) later in upcomming chapters.

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)