Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

5.0 C Operators

c

Uploaded by

samtjey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

5.0 C Operators

c

Uploaded by

samtjey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Operators in C


In C•• language, operators are symbols that represent operations to be performed on one or
more operands. They are the basic components of the C programming. In this article, we will
learn about all the built-in operators in C with examples.

What is a C Operator?
An operator in C can be defined as the symbol that helps us to perform some specific
mathematical, relational, bitwise, conditional, or logical computations on values and
variables. The values and variables used with operators are called operands. So we can say
that the operators are the symbols that perform operations on operands.

For example,
c = a + b;

Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The
addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.

Types of Operators in C
C language provides a wide range of operators that can be classified into 6 types based on
their functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
1. Arithmetic Operations in C
The arithmetic operators are used to perform arithmetic/mathematical operations on
operands. There are 9 arithmetic operators in C language:
S. No. Symbol Operator Description Syntax

Adds two
+ Plus a+b
1 numeric values.

Subtracts right
– Minus operand from left a–b
2 operand.

Multiply two
* Multiply a*b
3 numeric values.

Divide two
/ Divide a/b
4 numeric values.

Returns the
remainder after
% Modulus diving the left a%b
operand with the
5 right operand.

Used to specify
+ Unary Plus the positive +a
6 values.

Flips the sign of


– Unary Minus -a
7 the value.

Increases the
++ Increment value of the a++
8 operand by 1.

Decreases the
— Decrement value of the a–
9 operand by 1.

Example of C Arithmetic Operators


C
// C program to illustrate the arithmatic operators
#include <stdio.h>
int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);

return 0;
}

Output
a + b = 30
a - b = 20
a * b = 125
a / b = 5
a % b = 0
+a = 25
-a = -25
a++ = 25
a-- = 26

2. Relational Operators in C
The relational operators in C are used for the comparison of the two operands. All these
operators are binary operators that return true or false values as the result of comparison.
These are a total of 6 relational operators in C:
S. No. Symbol Operator Description Syntax

Returns true if
the left operand is
< Less than less than the right a<b
operand. Else
1 false
S. No. Symbol Operator Description Syntax

Returns true if
the left operand is
> Greater than greater than the a>b
right operand.
2 Else false

Returns true if
the left operand is
Less than or less than or equal
<= a <= b
equal to to the right
operand. Else
3 false

Returns true if
the left operand is
Greater than or greater than or
>= a >= b
equal to equal to right
operand. Else
4 false

Returns true if
== Equal to both the operands a == b
5 are equal.

Returns true if
!= Not equal to both the operands a != b
6 are NOT equal.

Example of C Relational Operators


C
// C program to illustrate the relational operators
#include <stdio.h>

int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}

Output
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1

Here, 0 means false and 1 means true.


3. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition in consideration. The result of the operation of a
logical operator is a Boolean value either true or false.
S. No. Symbol Operator Description Syntax

Returns true if
&& Logical AND both the operands a && b
1 are true.

Returns true if
|| Logical OR both or any of the a || b
2 operand is true.

Returns true if
! Logical NOT the operand is !a
3 false.

Example of Logical Operators in C


C
// C program to illustrate the logical operators
#include <stdio.h>

int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);

return 0;
}

Output
a && b : 1
a || b : 1
!a: 0

4. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the operands. The
operators are first converted to bit-level and then the calculation is performed on the
operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be
performed at the bit level for faster processing.
There are 6 bitwise operators in C:
S. No. Symbol Operator Description Syntax

Performs bit-by-
bit AND
& Bitwise AND a&b
operation and
1 returns the result.

Performs bit-by-
bit OR operation
| Bitwise OR a|b
and returns the
2 result.

Performs bit-by-
bit XOR
^ Bitwise XOR a^b
operation and
3 returns the result.

Flips all the set


Bitwise First
~ and unset bits on ~a
Complement
4 the number.

Shifts the number


<< Bitwise Leftshift in binary form by a << b
5 one place in the
S. No. Symbol Operator Description Syntax

operation and
returns the result.

Shifts the number


in binary form by
Bitwise
>> one place in the a >> b
Rightshilft
operation and
6 returns the result.

Example of Bitwise Operators


C
// C program to illustrate the bitwise operators
#include <stdio.h>

int main()
{

int a = 25, b = 5;

// using operators and printing results


printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
printf("a ^ b: %d\n", a ^ b);
printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);

return 0;
}

Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800

5. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a
value. The value on the right side must be of the same data type as the variable on the left side
otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide
multiple operations using single operator. These operators are called compound operators.
In C, there are 11 assignment operators :
S. No. Symbol Operator Description Syntax

Assign the value


Simple of the right
= a=b
Assignment operand to the
1 left operand.

Add the right


operand and left
operand and
+= Plus and assign a += b
assign this value
to the left
2 operand.

Subtract the right


operand and left
Minus and operand and
-= a -= b
assign assign this value
to the left
3 operand.

Multiply the right


operand and left
Multiply and operand and
*= a *= b
assign assign this value
to the left
4 operand.

Divide the left


operand with the
Divide and right operand and
/= a /= b
assign assign this value
to the left
5 operand.

Assign the
Modulus and remainder in the
%= a %= b
assign division of left
6 operand with the
S. No. Symbol Operator Description Syntax

right operand to
the left operand.

Performs bitwise
AND and assigns
&= AND and assign a &= b
this value to the
7 left operand.

Performs bitwise
OR and assigns
|= OR and assign a |= b
this value to the
8 left operand.

Performs bitwise
XOR and assigns
^= XOR and assign a ^= b
this value to the
9 left operand.

Performs bitwise
Rightshift and
Rightshift and
>>= assign this value a >>= b
assign
to the left
10 operand.

Performs bitwise
Leftshift and
Leftshift and
<<= assign this value a <<= b
assign
to the left
11 operand.

Example of C Assignment Operators


C
// C program to illustrate the assignment operators
#include <stdio.h>

int main()
{
int a = 25, b = 5;

// using operators and printing results


printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);
printf("a &= b: %d\n", a &= b);
printf("a |= b: %d\n", a |= b);
printf("a >>= b: %d\n", a >>= b);
printf("a <<= b: %d\n", a <<= b);

return 0;
}

Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a >>= b: 0
a <<= b: 0

6. Other Operators
Apart from the above operators, there are some other operators available in C used to
perform some specific tasks. Some of them are discussed here:
sizeof Operator
• sizeof is much used in the C programming language.
• It is a compile-time unary operator which can be used to compute the size of its
operand.
• The result of sizeof is of the unsigned integral type which is usually denoted by
size_t.
• Basically, the sizeof the operator is used to compute the size of the variable or
datatype.
Syntax
sizeof (operand)

To know more about the topic refer to this article.


Comma Operator ( , )
• The comma operator (represented by the token) is a binary operator that evaluates
its first operand and discards the result, it then evaluates the second operand and
returns this value (and type).
• The comma operator has the lowest precedence of any C operator.
• Comma acts as both operator and separator.
Syntax
operand1 , operand2

To know more about the topic refer to this article.


Conditional Operator ( ? : )
• The conditional operator is the only ternary operator in C++.
• Here, Expression1 is the condition to be evaluated. If the condition(Expression1)
is True then we will execute and return the result of Expression2 otherwise if the
condition(Expression1) is false then we will execute and return the result of
Expression3.
• We may replace the use of if..else statements with conditional operators.
Syntax
operand1 ? operand2 : operand3;

To know more about the topic refer to this article.


dot (.) and arrow (->) Operators
• Member operators are used to reference individual members of classes, structures,
and unions.
• The dot operator is applied to the actual object.
• The arrow operator is used with a pointer to an object.
Syntax
structure_variable . member;

and
structure_pointer -> member;

To know more about dot operators refer to this article and to know more about arrow(->)
operators refer to this article.
Cast Operator
• Casting operators convert one data type to another. For example, int(2.2000) would
return 2.
• A cast is a special operator that forces one data type to be converted into another.
• The most general cast supported by most of the C compilers is as follows − [ (type)
expression ].
Syntax
(new_type) operand;

To know more about the topic refer to this article.


addressof (&) and Dereference (*) Operators
• Pointer operator & returns the address of a variable. For example &a; will give the
actual address of the variable.
• The pointer operator * is a pointer to a variable. For example *var; will pointer to a
variable var.
To know more about the topic refer to this article.
Example of Other C Operators
C
// C Program to demonstrate the use of Misc operators
#include <stdio.h>

int main()
{
// integer variable
int num = 10;
int* add_of_num = &num;

printf("sizeof(num) = %d bytes\n", sizeof(num));


printf("&num = %p\n", &num);
printf("*add_of_num = %d\n", *add_of_num);
printf("(10 < 5) ? 10 : 20 = %d\n", (10 < 5) ? 10 : 20);
printf("(float)num = %f\n", (float)num);

return 0;
}

Output
sizeof(num) = 4 bytes
&num = 0x7ffe2b7bdf8c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000

Unary, Binary and Ternary Operators in C


Operators can also be classified into three types on the basis of the number of operands they
work on:
1. Unary Operators: Operators that work on single operand.
2. Binary Operators: Operators that work on two operands.
3. Ternary Operators: Operators that work on three operands.
Operator Precedence and Associativity in C
In C, it is very common for an expression or statement to have multiple operators and in these
expression, there should be a fixed order or priority of operator evaluation to avoid
ambiguity.
Operator Precedence and Associativity is the concept that decides which operator will be
evaluated first in the case when there are multiple operators present in an expression.
The below table describes the precedence order and associativity of operators in C. The
precedence of the operator decreases from top to bottom.
Precedence Operator Description Associativity

() Parentheses (function call) left-to-right

[] Brackets (array subscript) left-to-right

Member selection via object


. left-to-right
name

-> Member selection via a pointer left-to-right

Postfix increment/decrement (a
a++ , a– left-to-right
1 is a variable)

Prefix increment/decrement (a
++a , –a right-to-left
is a variable)

+,– Unary plus/minus right-to-left

Logical negation/bitwise
!,~ right-to-left
complement

Cast (convert value to


(type) right-to-left
temporary value of type)

* Dereference right-to-left

& Address (of operand) right-to-left

Determine size in bytes on this


sizeof right-to-left
2 implementation

3 *,/,% Multiplication/division/modulus left-to-right

4 +,– Addition/subtraction left-to-right


Precedence Operator Description Associativity

Bitwise shift left, Bitwise shift


<< , >> left-to-right
5 right

Relational less than/less than or


< , <= left-to-right
equal to

Relational greater than/greater


> , >= left-to-right
6 than or equal to

Relational is equal to/is not


== , != left-to-right
7 equal to

8 & Bitwise AND left-to-right

9 ^ Bitwise XOR left-to-right

10 | Bitwise OR left-to-right

11 && Logical AND left-to-right

12 || Logical OR left-to-right

13 ?: Ternary conditional right-to-left

= Assignment right-to-left

Addition/subtraction
+= , -= right-to-left
assignment

Multiplication/division
*= , /= right-to-left
assignment

Modulus/bitwise AND
%= , &= right-to-left
assignment

Bitwise exclusive/inclusive OR
^= , |= right-to-left
14 assignment
Precedence Operator Description Associativity

Bitwise shift left/right


<<=, >>= right-to-left
assignment

15 , expression separator left-to-right

Conclusion
The points we learned about the operator are as follows:
• Operators are symbols used for performing some kind of operation in C.
• There are six types of operators, Arithmetic Operators, Relational Operators, Logical
Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
• Operators can also be of type unary, binary, and ternary according to the number of
operators they are using.
• Every operator returns a numerical value except logical, relational, and conditional
operator which returns a boolean value (true or false).
• There is a Precedence in the operators means the priority of using one operator is
greater than another operator.

You might also like