Norway


In this C class, we’ll cover all supported C , explain their purpose with examples.

The operators help in mathematical and statistical computing. Hence, please read this with full focus and concentration.

In C programming, we may have to many operations on variables like addition, subtraction, division, increment, decrement, etc. And C language has many built-in operators to carry out such tasks. Please see below.

Table of Content

1. What are C Operators?

The operators are special symbols that instruct the compiler to perform a specific operation on the given variable(s).

There are mainly six types of operators. Let’s check them out one by one.

2. Types of Operators

2.1. Arithmetic Operators

These operators are used to perform simple arithmetic operations like addition, subtraction, product, quotient, remainder, etc.

SYMBOL USAGE
+ Used to add two or more variables (a+b)
Used to subtract two or more variables (a-b)
* Used to multiply two or more variables (a*b)
/ Used to divide two variables (a/b)
% Returns the remainder (a%b)
++ Increments by 1
Decrements by 1

2.2. Relational Operators

These operators are used to check a relation between two or more variables like ‘greater than,’ ‘less than,’ etc. If the result is positive, then the condition will succeed.

Please note that we’ll get to learn more about the conditions in further chapters.

The following table shows some relational operators supported in C.

SYMBOL USAGE
== Used to check if the values of two or more variables are equal.
!= It tests for the inequality, i.e., not similar.
> Tests if the left value is more than the right value.
< Tests if the left operand is smaller than the right one.
>= Tests if the left value is more than or equal to right-value.
<= Tests if the left operand is smaller than or equal to the right one.

2.3. Logical Operators

These are used to perform logical operations.

Here are some logical operators supported in C

SYMBOL USAGE
&& The Logical AND operator which checks if a condition is true for both the variables.
|| Logical OR operator which tests if the condition is true for either of the two variables.
! Logical NOT operator which reverses the logical state of an operand.

2.4. Bitwise Operators

These operators are used to perform operations on binary values.

This table would help you.

SYMBOL USAGE
& Used to perform binary AND operation
| Used to perform binary OR operation
^ Used to perform binary XOR operation
~ Used to perform binary complement operation
<< Used to perform binary Left Shift operation
>> Used to perform binary Right Shift operation

Now you must know the binary conversion of a decimal number. If not, this example will help you.

Let us assume two numbers 3 and 4. Now, in binary format, they are represented as

 8421 //Here every ‘1’ below these numbers will add that value to the number

A = 0011 //Hence for 3 it is 0+0+2+1 i.e. 3.

B = 0100 //And for 4 it is 0+1+0+0.

-------

A&B = 0000 //AND operation

A|B = 0111 //OR operation

Complement Operations :

~A = ~3 = 1100 ~B = ~4 = 1011 (Just replace 1 by 0 and 0 by 1)

Left Shift and Right Shift operations

A<<1 = 0110 A>>1 = 0001

(Just shift each binary element to the left or right by 1)

2.5. Assignment Operators

In C programming, variables get values by using the assignment operators.

Take an example, say, if you need to assign the value “7” to a variable known as the “count,” then you can do so with the following statement.

count = 7;

There are two subtypes of the assignment operators in C programming.

  • The simple operator ( = )
  • The compound operators (+=, -=, *=, /=, %=, &=, ^=)
SYMBOL USAGE
= count = 5;
Tjhe value 5 gets assigned to the variable count.
+= count += 5;
It means, count= count + 5
-= count -= 5;
It means, count= count – 5
*= count *= 5;
It means, count= count * 5
/= count /= 5;
It means, count= count / 5
%= count %= 5;
It means, count= count % 5
&= count &=5;
It means, count= count & 5
^= count ^= 5;
It means, count= count ^ 5

2.6. Miscellaneous Operators

These are some miscellaneous operators.

SYMBOL USAGE
sizeof() Gives the size of a variable
& Gives the address of a variable
* Pointer used to point to a variable
?: Used to check if a specific condition is true.

You may not see any programs or sample code in this chapter because we have been using these C operators in many of our other tutorials.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here