Welcome to your next tutorial in the C language, today, we’ll discuss about operators in the C language. We have already discussed about Variables in C language, and how we can store data in variables input by user, next we may need to update this data , or make various operations on the variables to produce desired results. This can be done through Operators in the C language.
Operators can be classified broadly as follows :
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Assignment Operator
- Conditional Operator
- Miscellaneous Operators
Now we’ll discuss about each category one by one, let us assume that we have declared two variables num1 and num2 and initialized them as shown below.
int num1 = 3; int num2 = 6;
Let's have another variable named res for storing result, so that we can analyze the value of result after applying each operator and understand their functioning.
But before this, we need to understand that there are basically two kinds of Operators depending on the number of operands they operate upon. They are :-
Unary Operators : which operate on only one operand.
Binary Operators : which requires two operands to be operated upon.
Now, we'll carry on with understanding different types of operator.
They are binary operators used to do arithmetic operations like addition , subtraction , multiplication , division etc.
+ used for addition like
res = num1 + num2;
This would add the two values ie 5 and 6 and store the result to variable res which is 9 in this case.
- used for subtraction like
res = num2 - num1;
This would subtract num1 from num2 and then store the result in variable res which is 3 in this case.
* used for multiplication like
res = num1 * num2;
This would multiply num1 and num2 and store the result in variable res which is 18 in this case.
/ used for division like
res = num2 / num1;
This would divide the two values ie num2 by num1 and then store the result in variable res which is 2 in this case.
% (modulus operator ) like
res = num2 % num1;
This operator will divide num2 by num1 and then gives us the REMAINDER which will be stored in variable res and it's value would be 0 in this case , since, when 6 is divided by 3, it would give remainder 0.
They are included in logical expressions and are of following types :
- Logical AND (&&)
- Logical NOT ( ! )
- Logical OR ( || )
Note that in Logical AND and Logical OR, we use the respective symbol two times as mentioned. We'll study their complete usage in the coming tutorials specially in Loops and Conditional Statements .
As the name suggests, they are used to depict relations among variables. Mostly they return either a true (1) value or a false (0) value. They are of the following types :
- Less than or equal to ( \<= )
- More than or equal to ( >= )
- Not equal to ( != )
Again, they would be better understood when we'll discuss about Conditional Statements and Loops, so we'll discuss them in the upcoming tutorials, just remember the types of operators for now.
They are used to assign values to a variables, we are already using this operator since we studied about assigning a value to our variables, so consider the following :
int num = 5;
Here ( = ) ie assignment operator is used to assign value 5 to variable num.
There are other operators too, but we have covered the most important operators here.
Unary + and Unary - are some of the operators you would see often. As the name suggests they operate on only one operand. To understand what they do, consider the following code snippet :
int num = 5; num = (+num); printf("Value of num after Unary + is %d n",&num); num = -num; printf("Value of num after Unary - is %d",&num);
Including all the necessary header files and other stuff required, it would produce the output as follows:
Value of num after Unary + is 5
Value of num after Unary - is -5
So, you understood it right Unary + doesn't changed the sign of the value, while unary - reversed the sign of integer value, so +5 become -5 , and it would be vice-versa if we have a negative value in variable.
In the next post, we'll study about Conditional statements and then about Loops in C and their usage, they would help us understand the concept of operators like logical and relational operator in much more detail. If you have any doubts/question, please feel free to flood them in the comments section, we'll help you !