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

Module 2-Operators in c

The document provides an overview of various operators in the C programming language, including arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, comma, and sizeof operators. It explains the functionality and syntax of each operator type, as well as concepts like operator precedence, type conversion, and type casting. Examples are included to illustrate how these operators work in practice.

Uploaded by

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

Module 2-Operators in c

The document provides an overview of various operators in the C programming language, including arithmetic, relational, equality, logical, unary, conditional, bitwise, assignment, comma, and sizeof operators. It explains the functionality and syntax of each operator type, as well as concepts like operator precedence, type conversion, and type casting. Examples are included to illustrate how these operators work in practice.

Uploaded by

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

OPERATORS IN C

An operator is defined as a symbol that specifies the mathematical, logical, or relational


operation to be performed.

The different operators available in C are:

➢ Arithmetic operators
➢ Relational operators
➢ Equality operators
➢ Logical operators
➢ Unary operators
➢ Conditional operators
➢ Bitwise operators
➢ Assignment operators
➢ Comma operator
➢ sizeof operator

1. ARITHMETIC OPERATORS
Arithmetic operators are used to perform arithmetic operations. Arithmetic operators
are evaluated from left to right.

Operation Operator Syntax


Multiply * op1*op1
Divide / op1/op2
Addition + op1+op2
Subtraction - op1/op2
Modulus % op1%op2

Modulus operator (%) returns the remainder of the division of two operands.

Example: 14%2=0

7%5=2

2. RELATIONAL OPERATORS

A relational operator compares two values. Expressions that contain relational


operators are called relational expression. The output of relational operation will be
either true(1) or false(0).
Operator Meaning Example
< Less than 3<5 gives 1
> Greater than 8>2 gives 1
<= Less than or equal to 10<=8 gives 0
>= Greater than or equal to 11>=8 gives 1
Relational operators are evaluated from left to right.

3. EQUALITY OPERATORS

Equality operators compare their operands for strict equality or inequality.


Operator Meaning
== Returns 1 if both operands are equal, else 0
!= Returns 1 if both operands are not equal, else 1

The equality operators have lower precedence than relational operators.

4. LOGICAL OPERATORS: C supports 2 logical operators. They are logical AND


(&&), logical OR (||) and logical NOT (!). The logical operators are evaluated from
left to right.

Logical AND Logical NOT

A B A&&B A !A
0 0 0 0 0
0 1 0 1 0
1 0 0
1 1 1

Logical OR

A B A||B
0 0 0
0 1 1
1 0 1
1 1 1

5. UNARY OPERATORS

Unary operators act on single operands. C supports three unary operators-


unary minus, increment and decrement operators

UNARY MINUS:

When a number is preceded by a minus sign, the unary minus negates it value. For
example , if a=10, then b=-(a) =>b=-10.

INCREMENT (++) and DECREMENT (--) OPERATOR:

The increment operator increases the value of its operand by 1.

Eg: a++; => a=a+1


The decrement operator decreases the value of its operand by 1.

Eg:- a--; => a=a-1;

Post increment (x++)& Pre increment (++x):

In post increment, the value of x is returned after it is incremented.

Eg: int x=10, y;

y=x++;

is equivalent to y=x;

x=x+1;

In pre increment, the value of x is incremented and then returned.

Eg: int x=10,y;

y=x++;

is equivalent to writing x=x+1;

y=x;

The same principle applies to pre decrement and post decrement.

Unary operators are evaluated from right to left.

6. CONDITIONAL OPERATOR

The conditional operator or the ternary operator (? :) is just like an if-else statement
that can be within expressions.

The syntax is: exp1? exp2 : exp3;

exp1 is evaluated first.If it is true then exp2 is evaluated else exp3 is evaluated.

Eg: int a=10, b=20, large;

large= a>b: a : b;

Conditional operator is called ternary operator since it takes 3 operands.

7. BITWISE OPERATORS:

Bitwise operators perform operations at the bit level. The bitwise operators expect
their operands to be integers.
The different bitwise operators are:
Bitwise AND, Bitwise OR, Bitwise XOR, Bitwise NOT, Shift operator.
Eg:
A=1010 1010 ; B=0101 0101
A&B=0000 0000
A|B = 1111 1111
~A= 0101 0101
A^B = 1111 1111

Shift operator
C has 2 shift operators: left shift (<<) and right shift (>>)operator.
The syntax is
Operand op num;

Left shift operator- The bits are shifted to the left. The most significant bit (MSB) is
lost and the least significant bit is set to 0.
Eg: A= 0001 1101
A<<1 gives 0011 1010
A<<2 gives 0111 0100

Right shift operator- The bits are shifted to the right. The most significant bit (MSB)
is set to 0 and the least significant bit is lost.
Eg: A= 0001 1101
A>>1 gives 0000 1110
A>>2 gives 0000 0111

8. ASSIGNMENT OPERATOR

The assignment operator (=) is responsible for assigning values to variables.

The assignment operator has right to left associativity.

So a = b = c = 10;

is evaluated as a = (b = ( c = 10 )));

C also supports shorthand assignment operator.

Syntax for shorthand operator is variable op = expression; where op is a binary arithmetic


operator.

Eg: x += y is equivalent to x = x + y;

The other shorthand assignment operators are +=, -=, *=, /= , %=, &=, ^= , <<=, >>=.

9. 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. The comma separated expressions are evaluated in the left to right
sequence. The comma operator has the least precedence.

Eg:

Int a=2, b = 3, x = 0;

x = (++a, b += a);

Now, the value of x=6

10. sizeof OPERATOR

The sizeof operator returns the size of the variable, data type or expression in bytes. It
is used to determine the amount of memory space that the variable/ data type or
expression will take.

Syntax: sizeof(variable/ data type/ expression);

Eg: 1) int a=10;

sizeof(a); will return 2.

2) sizeof (char) will retun 1.


OPERATOR PRECEDENCE

C operators have 2 properties: priority and associativity.

When an expression has more than one operator , then the relative properties of the operators
with respect to each other that determine the order in which the expression will be evaluated
is called precedence.

Associativity defines the direction in which the operator having the same precedence acts on
the operands. It can be either left – to- right or right- to – left.

The operator precedence chart is as below:


TYPE CONVERSION AND TYPE CASTING

Type conversion or typecasting of variables refers to changing a variable of one data type
into another.

Type conversion is done implicitly, whereas typecasting is to be done explicitly by the


programmer.

Type Conversion:

Type conversion in C is the process of converting one data type to another. The type
conversion is only performed to those data types where conversion is possible. Type
conversion is performed by a compiler. In type conversion, the destination data type can’t be
smaller than the source data type. Type conversion is done at compile time and it is also
called widening conversion because the destination data type can’t be smaller than the source
data type.

Eg: float x;

int y = 3;

x = y;

Now x = 3.0, as the integer value is automatically converted to its equivalent floating point
representation.

Type casting

It is also called forced conversion. it is user-defined. Here the user can typecast the result to
make it of a particular data type.

The syntax in C Programming:

(type) expression

type indicates the data type to which the final result is converted.
Eg: float salary = 10000.00;

int sal;

sal = (int) salary;

Now sal =10000.

When a floating point value is assigned to an integer value, the digits after decimal are
truncated. Hence, some data might be lost.

You might also like