0% found this document useful (0 votes)
12 views

Operators in C

The document provides an overview of various operators in the C programming language, categorizing them into groups such as arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, comma, and sizeof operators. It explains the syntax and functionality of each operator, along with examples to illustrate their usage. Additionally, it covers type conversion, type casting, operator precedence, and associativity in C.

Uploaded by

anantwork1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Operators in C

The document provides an overview of various operators in the C programming language, categorizing them into groups such as arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, comma, and sizeof operators. It explains the syntax and functionality of each operator, along with examples to illustrate their usage. Additionally, it covers type conversion, type casting, operator precedence, and associativity in C.

Uploaded by

anantwork1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

OPERATORS IN C

OPERATORS IN C
● C language supports a lot of operators to be used in expressions.
These operators can be categorized into the following major groups:
● Arithmetic operators
● Relational Operators
● Equality Operators
● Logical Operators
● Unary Operators
● Conditional Operators
● Bitwise Operators
● Assignment operators
● Comma Operator
● Sizeof Operator
OPERATION OPERATOR SYNTAX COMMENT RESULT*

Multiply * a * b result = a * b 27

Divide / a / b result = a / b 3
ARITHMETIC
OPERATORS Addition + a + b result = a + b 12

Subtraction - a - b result = a – b 6

Modulus % a % b result = a % b 0

* Consider three variables declared as


int a = 9, b = 3, result;
RELATIONAL OPERATORS
Also known as a comparison operator, it is an operator that compares two
values. Expressions that contain relational operators are called relational
expressions. Relational operators return true or false value, depending on
whether the conditional relationship between the two operands holds or not.
OPERATOR MEANING EXAMPLE

< LESS THAN 3 < 5 GIVES 1

> GREATER THAN 7 > 9 GIVES 0

<= LESS THAN OR EQUAL TO 100 >= 100 GIVES 1

>= GREATER THAN OR EQUAL TO 50 >=100 GIVES 0

EQUALITY OPERATORS
● C language supports two kinds of equality operators to compare their operands for
strict equality or inequality. They are equal to (==) and not equal to (!=) operator.

● The equality operators have lower precedence than the relational operators.

OPERATOR MEANING

== RETURNS 1 IF BOTH OPERANDS ARE EQUAL, 0 OTHERWISE


!= RETURNS 1 IF OPERANDS DO NOT HAVE THE SAME VALUE, 0 OTHERWISE
LOGICAL OPERATORS
● C language supports three logical operators. They are-
Logical AND (&&), Logical OR (||) and Logical NOT (!).
● As in case of arithmetic expressions, the logical expressions are evaluated from left to right.
A B A &&B A B A || B
A !A
0 0 0 0 0 0

0 1 0 0 1 1 0 1

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

UNARY OPERATORS
Unary operators act on single operands. C language supports three unary operators.
They are unary minus, increment and decrement operators.
When an operand is preceded by a minus sign, the unary operator negates its value.
The increment operator is a unary operator that increases the value of its operand by
1. Similarly, the decrement operator decreases the value of its operand by 1. For
example,
int x = 10, y;
y = x++;
is equivalent to writing
y = x;
x = x + 1; whereas, y = ++x;
is equivalent to writing
x = x + 1;
y = x;
CONDITIONAL OPERATOR

● The conditional operator (?:) is just like an if .. else statement that can be written within
expressions.
● The syntax of the conditional operator is
exp1 ? exp2 : exp3
Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes the result of the
expression, otherwise exp3 is evaluated and becomes the result of the expression. For
example,
large = ( a > b) ? a : b
● Conditional operators make the program code more compact, more readable.
● Conditional operator is also known as ternary operator as it is neither a unary nor a binary
operator; it takes three operands.
BITWISE OPERATORS
● Bitwise operators perform operations at bit level. These operators include: bitwise AND, bitwise
OR, bitwise XOR and shift operators.
● The bitwise AND operator (&) is a small version of the boolean AND (&&) as it performs
operation on bits instead of bytes, chars, integers, etc.
● The bitwise OR operator (|) is a small version of the boolean OR (||) as it performs operation on
bits instead of bytes, chars, integers, etc.
● The bitwise NOT (~), or complement, is a unary operation that performs logical negation on
each bit of the operand. By performing negation of each bit, it actually produces the 1’s
complement of the given binary value.
● The bitwise XOR operator (^) performs operation on individual bits of the operands.

BITWISE SHIFT OPERATORS


In bitwise shift operations, the digits are moved, or shifted, to the left or right. The CPU
registers have a fixed number of available bits for storing numerals, so when we perform
shift operations; some bits will be "shifted out" of the register at one end, while the same
number of bits are "shifted in" from the other end.
In a left arithmetic shift, zeros are shifted in on the right. For example;
unsigned int x = 11000101;
Then x << 2 = 00010100
If a right arithmetic shift is performed on an unsigned integer then zeros are shifted on
the left.
unsigned int x = 11000101;
Then x >> 2 = 00110001
ASSIGNMENT OPERATORS
The assignment operator is responsible for assigning values to the variables. While the
equal sign (=) is the fundamental assignment operator, C also supports other
assignment operators that provide shorthand ways to represent common variable
assignments. They are shown in the table.

OPERATOR SYNTAX EQUIVALENT TO

/= variable /= expression variable = variable / expression

\= variable \= expression variable = variable \ expression


*= variable *= expression variable = variable * expression
+= variable += expression variable = variable + expression
-= variable -= expression variable = variable - expression
&= variable &= expression variable = variable & expression
^= variable ^= expression variable = variable ^ expression
<<= variable <<= amount variable = variable << amount
>>= variable >>= amount variable = variable >> amount
COMMA OPERATOR
● The comma operator in C takes two operands. It works by evaluating the first and discarding its
value, and then evaluates the second and returns the value as the result of the expression.
● Comma separated operands when chained together are evaluated in left-to-right sequence with
the right-most value yielding the result of the expression.
● Among all the operators, the comma operator has the lowest precedence. For example,
int a=2, b=3, x=0;
x = (++a, b+=a);
Now, the value of x = 6.

SIZEOF OPERATOR
● sizeof is a unary operator used to calculate the sizes of data types.
● It can be applied to all data types.
● The operator returns the size of the variable, data type or expression in bytes.
● 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take. For example,
sizeof(char) returns 1, that is the size of a character data type. If we have,
int a = 10;
unsigned int result;
result = sizeof(a);
then result = 2,
TYPE CONVERSION AND TYPE CASTING
● Type conversion and type casting of variables refers to changing a variable of one data type
into another.
● While type conversion is done implicitly, casting has to be done explicitly by the programmer.
We will discuss both of them here.
● Type conversion is done when the expression has variables of different data types. So to
evaluate the expression, the data type is promoted from lower to higher level where the
hierarchy of data types can be given as: double, float, long, int, short and char.
● For example, type conversion is automatically done when we assign an integer value (lower
level) to a floating point (higher level) variable. For example,

float x;
int y = 3;
x = y;
Now, x = 3.0,

● Type casting is also known as forced conversion. It is done when the value of a
higher data type has to be converted in to the value of a lower data type. For example,
we need to explicitly type cast an integer variable into a floating point variable.
float salary = 10000.00;
int sal;
sal = (int) salary;
● Typecasting can be done by placing the destination data type in parentheses
followed by the variable name that has to be converted.
● It is always a good practice to include the destination data type in
parentheses followed by the variable name that has to be converted for both
Typeconversion (lower to higher level) and Typecasting (higher to lower level).
Operator Precedence and Associativity

● C operators have two properties: Precedence and Associativity


● Operator Precedence (Priority): The order in which operators are applied
to operands during the evaluation of an expression. It is important when an
expression has more than one operators.
● Operator Associativity: Associativity defines the direction in which
operators having the same precedence acts on operands.
● For example:
a = b = c = 10; is evaluated as
(a = (b = (c = 10))); i.e. the evaluation is done from right to left direction.

x = 3 * 4 * 10 + 5 * 6 ; is evaluated as
x = 12 *10 + 5 * 6 = 120 + 5 * 6 = 120 + 30 = 150, i.e. the evaluation is done
from left to right based on precedence order.
Operator Precedence and Associativity
● It is always a good practice to include use braces () in the expressions while
writing the C programs.

You might also like