Home > C > Usages and Definitions of All Operators in C

Usages and Definitions of All Operators in C

There is a list of all operators in C.

  1. Assignment Operator
  2. Arithmetic Operators
  3. Cast Operator
  4. Increment and Decrement Operators
  5. Abbreviated Assignment Operators
  6. Relational Operators
  7. Logical Operators

  1. Precedence of Operators


I. Assignment Operator

Equal sign (=) is used as assignment operator in C and it must be read as “is assigned the value of”.

1
a = b;

That means: a is assigned value of b. There is also this usage that allows you to make multiple assignments like this:

1
r=c=o=153;

All of the variables are equalized to 153. The precedence of assignment operator is right to left. That means first o is equalized to 153, then c is assigned value of o which is 153, finally r is assigned value of c.

Example 1.0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main(){
int a,b,c=4;

a=94;
b=351;

printf("1. value of a: %d\n", a);
printf("1. value of b: %d\n", b);
printf("1. value of c: %d\n\n ", c);

a=b;

printf("2. value of a: %d\n", a);
printf("2. value of b: %d\n", b);
printf("2. value of c: %d\n\n ", c);

c=a=6;
printf("3. value of a: %d\n", a);
printf("3. value of b: %d\n", b);
printf("3. value of c: %d\n\n ", c);

return 0;
}

Output of Example 1.0:

1
2
3
4
5
6
7
8
9
10
11
1. value of a: 94
1. value of b: 351
1. value of c: 4

2. value of a: 351
2. value of b: 351
2. value of c: 4

3. value of a: 6
3. value of b: 351
3. value of c: 6


II. Arithmetic Operators

Operator

Description

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Remainder ( or Modulus )

Example 2.0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int main(){
int x=5,y=8,z=12,t=4;
float a,b,c,d,e;

a=x+y;
b=z-t;
c=x*4;
d=y/t;
e=y%5;

printf("Result of equation A is: %.2f\n",a);
printf("Result of equation B is: %.2f\n",b);
printf("Result of equation C is: %.2f\n",c);
printf("Result of equation D is: %.2f\n",d);
printf("Result of equation E is: %.2f",e);

return 0;
}

Output of Example 2.0:

1
2
3
4
5
Result of equation A is: 13.00
Result of equation B is: 8.00
Result of equation C is: 20.00
Result of equation D is: 2.00
Result of equation E is: 3.00


III. Cast Operator

Sometimes results of arithmetic operations can be unexpected. For example; division of two integer number may return a float number, but there will be an information loss because we cannot get a float number from integers. So, what should we do?

As you see from the title, we should use the Cast operator. The Cast operator allows you to change the type of number temporarily before the next computation.

Example 3.0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int main(){
int x=5,y=8;
float a,b,c;

a=y/x;
printf("Result of equation A is: %.2f\n",a);

b=(float)y/x;
printf("Result of equation B is: %.2f\n",b);

c=y/x;
printf("Result of equation C is: %.2f\n",b);


return 0;
}

Output of Example 3.0:

1
2
3
Result of equation A is: 1.00
Result of equation B is: 1.60
Result of equation C is: 1.00

As you see, we changed the type of result of y/x in line 10 to float via (float) operator. You can define the (type) operator as you need.


IV. Increment and Decrement Operators

Operator

Description

++

Increment Operator

--

Decrement Operator

These operators are used to make operations easier and faster. Increment operator adds 1; decrement operator subtracts 1 from the number.

4.1 ) Preincrementation and Predecrementation

Usage

Equals To

x=++a -b;

a=a+1;
x=a-b;

y=--c -d;

c=c-1;
y=c-d;

4.2 ) Postincrementation and Postdecrementation

Usage

Equals To

x=a++ -b;

x=a-b;
a=a+1;

y=c-- -d;

y=c-d;
c=c-1;

Example 4.0: All usages of increment and decrement operators

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main(){
int x=5;

printf("1. Value= %d\n",x);

printf("2. Value= %d\n",++x);

printf("3. Value= %d\n",x++);

printf("4. Value= %d\n",x);

printf("5. Value= %d\n",--x);

printf("6. Value= %d\n",x--);

printf("7. Value= %d\n",x--);

printf("8. Value= %d\n",x);

return 0;
}

Output of Example 4.0:

1
2
3
4
5
6
7
8
1. Value= 5
2. Value= 6
3. Value= 6
4. Value= 7
5. Value= 6
6. Value= 6
7. Value= 5
8. Value= 4


V. Abbreviated Assignment Operators

Operator

Description

+=

Assign by Sum

-=

Assign by Subraction

*=

Assign by Product

/=

Assign by Quotient

%=

Assign by Remainder

Usages of Abbreviated Assignment Operators:

Usage

Equals To

x+=5;

x=x+5;

x+=d;

x=x+d;

y-=7;

y=y-7;

r = c -= o + g;

r=(c=(c-(o+g)));

r = c -= o + g;

c=(c-(o+g));
r=c;

Example 5.0: Proving equality of expressions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main(){
int r=5,c=4,o=6,g=3;

r = c  += o - g;
printf("Value of First Equation: %d\n",r);
/*We are making the same operations,
with different variables and equations*/

int x=5,y=4,z=6,t=3;

y=y+(z - t);
x=y;
printf("Value of Second Equation: %d\n",x);

return 0;
}

Output of Example 5.0:

1
2
Value of First Equation: 7
Value of Second Equation: 7


VI. Relational Operators

Operator

Description

<

is less than

>

is greater than

<=

is less than or equal to

>=

is greater than or equal to

!=

is NOT equal to

==

is equal to

These are can be used as conditional operators in C. If the condition is true then it returns 1, if condition is false then it return zero.

Example 6.0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

int main(){
int r,c=6,o=5;

r=c>o;
printf("Value of First Equation: %d\n",r);

r=c<o;
printf("Value of Second Equation: %d\n",r);

r=c==o;
printf("Value of Third Equation: %d\n",r);

return 0;
}

Output of Example 6.0:

1
2
3
Value of First Equation: 1
Value of Second Equation: 0
Value of Third Equation: 0


VII. Logical Operators

Operator

Description

&&

Logical AND

||

Logical OR

!

Logical Negation ( NOT )

To compare exist conditions and to generate new conditions we use this logical operators. The associativity from highest to the lowest is ! (NOT), && (AND), || (OR), but you can use parenthesis to change the hierarchy.
The relational operators have higher precedence than logical operators.

Example 7.0:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>

int main(){
int a=5,c=3,d=6,i=5;

if(a==d && c>d){
       printf("The both conditions are true!\n");
}else{
      printf("At least one of the conditions is false!\n");      
}

if(a==i || c>d){
       printf("At least one of the conditions is true!\n");
}else{
      printf("All conditions are false!\n");      
}

if(a==i && c<d){
       printf("The both conditions are true!\n");
}else{
      printf("At least one of the conditions is false!\n");    
}

if(a!=i){
       printf("The numbers are NOT equal!\n");
}else{
      printf("The numbers are equal!\n");    
}

return 0;
}

Output of Example 7.0:

1
2
3
4
At least one of the conditions is false!
At least one of the conditions is true!
The both conditions are true!
The numbers are equal!



Precedence of Operators

Precedence

Operator

Associativity

1

( )

Innermost First

2

+
-
++

(type)
!

Right to Left (Unary)

3

*
/
%

Left to Right

4

+
-

Left to Right

5

<
<=
>
>=

Left to Right

6

==
!=

Left to Right

7

&&

Left to Right

8

||

Left to Right

9

( )

Right to Left

Bookmark and Share
Categories: C Tags: , , ,
  1. No comments yet.
  1. No trackbacks yet.
eXTReMe Tracker