C Operators: Arithmetic, Relational, Equality, Logical, Unary, Conditional, and Bitwise, Assignments of C programming

A detailed explanation of various operators in C programming language. It covers arithmetic, relational, equality, logical, unary, conditional, and bitwise operators. Each operator is explained with its meaning, syntax, and example. The document also includes information on type conversion and type casting.

Typology: Assignments

2014/2015

Uploaded on 01/28/2022

live-kotak
live-kotak 🇮🇳

1 document

1 / 52

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT - 1
Program
1
BCA : 1
Sub : 05BC1103
(PROGRAMMING IN C -1 )
Unit : 3
Operators and IO Statements
Dr.Ashwin R. Dobariya
Associate Professor, FCA,MU
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34

Partial preview of the text

Download C Operators: Arithmetic, Relational, Equality, Logical, Unary, Conditional, and Bitwise and more Assignments C programming in PDF only on Docsity!

UNIT - 1

Program

1

BCA : 1

Sub : 05BC

( PROGRAMMING IN C -1 )

Unit : 3

Operators and IO Statements

Dr.Ashwin R. Dobariya Associate Professor, FCA,MU

OPERATORS IN C

Q:-Write detail short note on Operators of C

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions.

Operands are expressions or values on which an operator operates or works (often constants or variables)

(1) ARITHMETIC OPERATORS

These operators are used to perform various types of

arithmetic operations.

OPERATION OPERATOR SYNTAX COMMENT RESULT Multiply * a * b result = a * b 27 Divide / a / b result = a / b 3 Addition + a + b result = a + b 12 Subtraction - a - b result = a – b 6 Modulus % a % b result = a % b 0

Value of a=9, b=

(2) Relational Operators (>, >= , < , <= )

It is 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.

(2) Relational Operators (>, >= , < , <= )number :For example : Check whether first number is greater than secondVoid main(){int x = 5 , y = 3;Clrscr();if (x > y)printf(“\n x is greater than y”);elseprintf(“\n y is greater than x “);getch(); }

(3) 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 (priority)

than the relational operators.

OPERATOR MEANING == RETURNS OTHERWISE 1 IF BOTH OPERANDS ARE EQUAL, 0 != RETURNS 1 IF OPERANDS DO NOT HAVE THE SAMEVALUE, 0 OTHERWISE

(4) Logical Operators (&& , || ,! )

 Logical operators are generally used when we want to

check more than one condition using if statement.

 C language supports three logical operators.

 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

Dr.Ashwin Dobariya

Logical Operators && , ||,!

11

&& (AND) Operator If (condition1 && condition2 ) True part else False Part Expression 1 / Condition

Expression 2 / Condition

Result True True True True False False False True False False False False

|| (OR) Operator If (condition1 || condition2) True part else False Part Expression 1 / Condition

Expression 2 / Condition

Result True True True True False True False True True False False False

(4) Logical Operators (&& , || ,! )[Passing marks 40 out of 100]For example 2 : Input five subject marks and check whether student is pass or fail using AND and OR operator. void main( ) { int s1,s2,s3,s4,s5,tot,per; clrscr(); printf scanf(“(“%d%d%d%d%\n Enter marks of five subjects :d”,&s1,&s2,&s3,&s4,&s5);”); tot = s1 + s2 + s3 + s4 + s5; per = tot / 5.0; // Using AND Operator if(s1 >=40 && s2 >= 40 && s3 >= 40 && s4 >=40 && s5>=40 ) else^ printf(“\n Result is^ Pass”); // Using OR Operator^ printf(“\n Result is^ Fail”); if(s1 < 40 || s2 <40 || s3 < 40 || s4 < 40 || s5 < 40 ) printf(“\n Result is Fail ”); else printf(“\n Result is Pass”);

}^ getch ( ) ;

(5) Unary Operators (++ , -- , - )A operator which operates on a single operand , is known as unary operator.C language supports three unary operators.(1) minus ( - ) , (2) increment (++) (3) decrement (--) operators.When an operand is preceded by a minus sign, the unary operator negates its value. E.g int x = -The increment operator is a unary operator that increases the value of its operand by 1.For example : int x = 5; // To increment by 1 value in xx = x + 1 OR x ++ OR x += 1Similarly, the decrement operator decreases the value of its operand by 1. For example,For example : int x = 5; // To decrement by 1 value in xx = x - 1 OR x - - OR x -= 1

Dr.Ashwin Dobariya

(prefix / postfix) Increment & Decrement Operators

 (2) Postfix operator for example X++ , X--

 For example : int X = 5, Y;

▻ Y = X++ (Internally this statement executes with two steps)

▻ Step 1 : Y = X

▻ Step : 2 : X = X + 1

 Output : X = 6, Y = 5

16

Y = X++

Y = X

X = X + 1

Y = X++

Y = X Y = 5

X=5+1 X = 6

First Assignment Process

SecondIncrement

(6) Conditional Operators (? : )

The conditional operator 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 (condition) is evaluated first. If it is true then exp

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, and safer to use as it is easier both to check and

guarantee that the arguments that are used for evaluation.

 Conditional operator is also known as ternary operator as it is

neither a unary nor a binary operator; it takes three operands.

(6) Conditional Operators (? : )

 For example : Displays value of maximum out of two

numbers

// Using conditional operator

void main()

int x,y;

clrscr();

printf(“\n Enter any two numbers “);

scanf(“%d%d”,&x,&y);

(x>y)? printf(“x is max” : printf(“y is max”) ;

getch();

(7) Bitwise Operators ( & , | , ~ , ^ , >> , << )

  • C provides six bitwise operators for manipulating the individual bits in an integer quantity.
  • Bitwise operators expect their operands to be integer quantities and treat them as bit sequence.
  • Bitwise negation ( ~ ) is a unary operator that complements the bits in its operands. Operators Meaning Associativity & bitwiseAND^ LefttoRight | bitwiseOR^ LefttoRight ~ 1’sComplement^ LefttoRight ^ bitwiseExclusiveOR^ LefttoRight << shiftleft^ **LefttoRight

shiftright**^ LefttoRight