Last Update: Apr 27, 2024 | Janet

C operators are used to manipulating data and variables in programming. In simple words, C operators are used to performing mathematical or logical operations on variables or data.

- The most popular operators in c programming are +, -, *, /, =, &, <, >, etc.
- Each operator has its own unique symbol and functionality.
- C operators are classified into a number of categories which are listed below.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment and decrement operators
- Conditional Operators
- Bitwise Operators
- Special Operators

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

- + [Addition or unary plus]
- - [Subtraction or unary minus]
- * [Multiplication]
- / [Division]
- % [Modulo division]

**Example:-**

*#include<stdio.h>*

* int main()*

* {*

* int m=13, n=3, res;*

* res=m+n;*

* printf("m+n=%d\n",res);*

* res=m-n;*

* printf("m-n=%d\n",res);*

* res=m*n;*

* printf("m*n=%d\n",res);*

* res=m/n;*

* printf("m/n=%d\n",res);*

* res=m%n;*

* printf("Remainder is = %d\n",res);*

* return 0;*

* }*

**Output**

*m+n=16*

*m-n=9*

*m*n=39*

*m/n=4*

*Remainder is = 1*

**Explanation**

- The Arithmetic operators +, -, * and / performs an 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 marred res variable as int type so it will store only the complete part and ignore the fractional part which is .333 in this condition.
- If we want to store complete results then we can mark the res variable as float data type.

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

- If the result of the comparison is true then it returns 1 and if the result of the 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 below

- == [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]

**Example:-**

*#include<stdio.h>*

* int main()*

* {*

* int age=19,check_res;*

* if(age>=18)*

* {*

* printf("You are a valid voter\n");*

* }*

* if(age<18)*

* {*

* printf("You are a not eligible for voting\n" );*

* }*

* check_res = (age>=18)*

* printf("The check point value = %d",check_res);*

* return 0;*

* }*

**Output**

*You are a valid voter*

*The check point value = 1*

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

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

- There are 3 logical operators in the C programming language.
- Logical expression returns 1 if the condition is true and returns 0 if the condition is false.

**&&**[Logical AND, True only if all conditions are true]**||**[Logical OR, True if at least one condition is true]**!**[Logical NOT, True only if expression result is 0]

**Example**

*if m=9 and n=3 then, expression ((m>n)&&(n<m)) will be equal to 1*

*if m=9 and n=3 then, expression ((m>n)&&(m<n)) will be equal to 1*

*If m=9 then, expression !(m==9) will be equal to 0*

**Example:-**

*#include<stdio.h>*

* int main()*

* {*

* int a=16,b=18,result;*

* result = (a<b) && (b>a);*

* printf("(a<b) && (b>a) is equal=%d\n",result);*

* result = (a>b) || (b<a);*

* printf("(a>b) || (b<a) is equal=%d\n",result);*

* *

* result = !(a==16);*

* printf("!(a==16) is equal=%d\n",result);*

* return 0;*

* }*

**Output**

*(a<b) && (b>a) is equal=1*

*(a>b) || (b<a) is equal=0*

*!(a==16) is equal=0*

**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 expressions 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 expressions 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 are used to assigning 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.

**=**[m+n or n+m]**+=**[m+=n or m=m+n]**-=**[m-=n or m=m-n]***=**[m*=n or m=m*n]**/=**[m/=n or m=m/n]**%=**[m%=n or m=m%n]

**Example:-**

*#include<stdio.h>*

* int main()*

* {*

* int a=5;*

* int b=10;*

* int c=4;*

* int d=20;*

* int e=30;*

* a += 5*

* printf("Now a = %d",a);*

* b -= 2*

* printf("Now b = %d",b);*

* c *= 6*

* printf("Now c = %d",c);*

* d /= 5*

* printf("Now d = %d",d);*

* e %= 7*

* printf("Now e = %d",e);*

* return 0;*

* }*

**Output**

*Now a = 10*

*Now b = 8*

*Now c = 24*

*Now d = 4*

*Now e = 2*

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 in C are also known as ternary operators and represented as the pair of? :

Condition? Expression1: Expression2

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

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

**Example:-**

*#include<stdio.h>*

* int main()*

* {*

* int a=20;*

* int b=30;*

* int x;*

* x=(a<b)?a:b;*

* printf("The value of x = %d",x);*

* return 0;*

* }*

**Output**

*The value of x = 20*

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

**Example:-**

*#include<stdio.h>*

* int main()*

* {*

* int a=20;*

* int b=30;*

* int x;*

* x=(a>b)?a:b;*

* printf("The value of x = %d",x);*

* return 0;*

* }*

**Output**

*The value of x = 30*

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

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

**&**[bitwise AND]**|**[bitwise OR]**^**[bitwise exclusive OR]**<<**[shift left]**>>**[shift right]

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

**,**[comma operator]**sizeof**[size of operator]*** and &**[pointer operator]**. and ->**[member selection operator]

The comma operator is used to link several related expressions together. See below example of how we have implemented related expressions together.

**Example**

int a,b, c=10,d;

In the 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.

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

**Example:-**

*int age;*

*a = sizeof(age);*

*b = sizeof(long int);*

*c = sizeof(132L);*

*d = sizeof(char);*

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

How auto increment is implemented in SQL?

Can you have multiple WHERE statements in SQL?

Which of these tags are all table tags?

Does every table need a primary key?

How to write insert query in SQL?

Is left outer join and full join same?

How to make blockquote in HTML?

Can we use ANY and ALL operator without WHERE?

What Are The Advantages Of Auto Variables?

What is position sticky in CSS?

What is the future of quantum computing in Singapore?

What is the use of address tag in HTML?

How to create a constraint in SQL?

Advertise With Us

Email Us: __advertise@gdatamart.com __

Support Our Effort

Donate Us: Support to GDATAMART

© 2024 GDATAMART.COM (All Rights Reserved)