Quantcast
Channel: Geek Files
Viewing all articles
Browse latest Browse all 10

C Tutorial – Operators in C

$
0
0

An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence.

Category of operators

Unary Operators
A unary operator is an operator, which operates on one operand.

Binary
A binary operator is an operator, which operates on two operands

Ternary
A ternary operator is an operator, which operates on three operands.

Following are the different types of Operators

Arithmetic Operator
The arithmetic operator is a binary operator, which requires two operands to perform its operation of arithmetic. Following are the arithmetic operators that are available

Operator Description
+ Addition
- Subtraction
/ Division
* Multiplication
% Modulo or remainder

At the time of using the ‘/’ division operator on two integers, the result will also be an integer. E.g. 100/8 will result in 12 and not 12.50.




Unary Operators

A unary operator is an operator, which operates on one operand, i.e. it operates on itself.

Following are the unary operators available under C

!  &  *  -  +  ~

Increment and Decrement Operators

These operators also fall under the broad category or unary operators but are quite distinct than unary minus.

The increment and decrement operators are very useful in C language. They are extensively used in for and while loops. The syntax of these operators is given below.

++
++

The ++ operator increments the value of the variable by one, whereas the – operator decrements the value of the variable by one.

These operators can be used in either the postfix or prefix notation as follows:

Postfix: a++ or a–
Prefix: ++a or –a

Postfix notation

In the postfix notation, the value of the operand is used first and then the operation of increment or decrement takes place, e.g. consider the statements below:

	int a,b;
	a = 10;
	b = a++;
	printf(“%d\n”,a);
	printf(“%d\n”,b);

Program Output
10
11

Prefix notation

In the prefix notation, the operation of increment or decrement takes place first after which the new value of the variable is used.

	int a,b;
	a = 10;
	b = ++a;
	printf(“%d\n”,a);
	printf(“%d\n”,b);

Program Output
11
11

Relational Operators

Relational operators compare between two operands and return in terms of true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional & looping statements.

Following are the various relational operators

< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to

Closely related to the relational operators is the equality operator as follows:

== Equal to != Not equal to

Example:

Suppose we have three integer variables a, b and c with values 1,2 and 3 respectively.

Expression Returns Meaning
A<b 1 True
(a+b)>=c 1 True
(b+c)>(a+5) 0 False
(c!=3) 0 False
B==2 1 True

Logical Operators

A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available in the C language.

Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT

Truth table for Logical AND

1 1 1
1 0 0
0 1 0
0 0 0

Truth table for Logical OR

1 1 1
1 0 1
0 1 1
0 0 0

Example

Let us take three variables a, b, and c and assume a = 5,b=10 and c=7

a&gt;b && a+b&gt;c

Here a>b will return false hence 0 and a+b>c will return true hence 1. Therefore, looking at the truth table 0 AND 1 will be 0. From this we can understand, that this expression will evaluate to false.

Let us look at the same example using Logical OR (||)

a&gt;b || a+b&gt;c

Here a>b will return false hence 0 and a+b>c will return true hence 1. Therefore, looking at the truth table 0 OR 1 will be 1. From this we can understand, that this expression will evaluate to true.

The Logical NOT operator (!)

The ! (Logical NOT) operator is a unary operator. This operator inverses a logical result.

E.g. if a = 5 and b = 7, then

	!(a == b)

will evaluate to be true. This expression works as follows, a==b evaluates to be false, now because of the ! (NOT) operator, the false is inverted to be true.




Assignment Operator

An assignment operator (=) is used to assign a constant or a value of one variable to another.

Example:

a = 5;
b = a;
interestrate = 10.5
result = (a/b) * 100;

Important Note:

Remember that there is a remarkable difference between the equality operator (==) and the assignment operator (=). The equality operator is used to compare the two operands for equality (same value), whereas the assignment operator is used for the purposes of assignment.

Multiple assignments:

You can use the assignment for multiple assignments as follows:

a = b= c = 10;

At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.

Arithmetic Assignment Operators

Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.

Operators

+=

-=

*=

/=

%=

Example Expands as
Sum+=3 Sum = sum + 3
Count-=4 Count -= 4
Factorial*=num Factorial = factorial * num
Num/=10 Num = num /10
A%=3 A = a % 3

Conditional Operator

A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.

Its general structure is as follows:

Expression1 ? expression 2 (True Section): expression3 (False Section)

Example:

a=3,b=5,c;
 
c = (a&gt;b) ? a+b : b-a;

The variable c will have the value 2, because when the expression (a>b) is checked, it is evaluated as false. Now because the evaluation is false, the expression b-a is executed and the result is returned to c using the assignment operator.

Precedence of Operators

Operator Associavity
Unary Right to Left
Arithmetic Operators Left to Right
Relational Operator Left to Right
Equality Operator Left to Right
Logical Operator Left to Right
Conditional Operator Right to Left
Assignment Operator Right to Left
Comma operator Right to Left

Type Conversion

When an operator’s operands are of different data types, C automatically converts them to the same data type. This can affect the results of mathematical expressions. This is called type conversion. In C type conversion can be done by two ways, viz.

Automatic Conversion (Arithmetic Promotion)
When a value is converted to a higher type, it is said to be promoted. Let’s look at the specific rules that govern the evaluation of mathematical expressions.

Rule 1: chars, shorts, unsigned shorts are automatically promoted to int.
Rule 2: When an operator works with two values of different data types, the lower data type is promoted to a higher data type.

Type Casting
Type casting means to convert a higher data type to a lower data type. For the purpose of type casting we require to use the type case operator, which lets you manually promote or demote a value. It is a unary operator which appears as the data type name followed by the operand inside a set of parentheses. E.g.

	val = (int)number;

This was about Operators in C, in the next C Tutorial you will learn about input and output in C.

Subscribe now to receive updates when a new C tutorial is released.


Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images