Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Similar documents
Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators in C. Staff Incharge: S.Sasirekha

Chapter 3: Operators, Expressions and Type Conversion

Operators & Expressions

Arithmetic Operators. Portability: Printing Numbers

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators. Java operators are classified into three categories:

A flow chart is a graphical or symbolic representation of a process.

Data Types and Variables in C language

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

UNIT- 3 Introduction to C++

Operators and Expressions:

Information Science 1

JAVA OPERATORS GENERAL

Programming in C++ 6. Floating point data types

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Expressions and Precedence. Last updated 12/10/18

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

UNIT 3 OPERATORS. [Marks- 12]

SECTION II: LANGUAGE BASICS

Engineering Computing I

Operators in java Operator operands.

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Department of Computer Science

Programming for Engineers Iteration

Chapter 4. Operations on Data

Informatics Ingeniería en Electrónica y Automática Industrial

1.3b Type Conversion

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Infix to Postfix Conversion

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Programming in C++ 5. Integral data types

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Java enum, casts, and others (Select portions of Chapters 4 & 5)

Fundamentals of Programming

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Review of the C Programming Language

Computers Programming Course 6. Iulian Năstac

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

More Programming Constructs -- Introduction

Review of the C Programming Language for Principles of Operating Systems

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Declaration and Memory

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Expression Evaluation and Control Flow. Outline

Type Conversion. and. Statements

Fundamental of Programming (C)

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

CT 229. Java Syntax 26/09/2006 CT229

Chapter 3 Structure of a C Program

Chapter 7. Expressions and Assignment Statements ISBN

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Expression and Operator

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Operators and Expressions

DEPARTMENT OF MATHS, MJ COLLEGE

Prepared by: Shraddha Modi

Chapter 7. Additional Control Structures

Chapter 2: Using Data

Expressions & Assignment Statements

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Reserved Words and Identifiers

Information Science 1

Prof. Navrati Saxena TA: Rochak Sachan

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Programming for Engineers: Operators, Expressions, and Statem

CHAPTER 3 Expressions, Functions, Output

Expressions and Assignment Statements

ME 461 C review Session Fall 2009 S. Keres

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

COMP2611: Computer Organization. Data Representation

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning


Chapter 7. Expressions and Assignment Statements

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

LECTURE 3 C++ Basics Part 2

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Part I Part 1 Expressions

Definition: Data Type A data type is a collection of values and the definition of one or more operations on those values.

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

CS2141 Software Development using C/C++ C++ Basics

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

COMP Primitive and Class Types. Yi Hong May 14, 2015

C Programming Language (Chapter 2 of K&R) Variables and Constants

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Introduction to C. Systems Programming Concepts

Arithmetic and Bitwise Operations on Binary Data

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

QUIZ: What value is stored in a after this

Transcription:

Operators and Type Conversion By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Introduction An operator is a symbol which represents a particular operation that can be performed on some data. The data is called as an operand. The operator thus operates on an operand. Operators can be classified as unary, binary and ternary depending on the number of operands, i.e. one, two or three respectively.

CLASSIFICATION:OPERATORS IN C

DIFFERENT OPERATORS

ARITHMETIC OPERATOR There are three types of arithmetic operators in C:unary, binary and ternary. Binary operators: C provides five basic arithmetic binary operators. % (Mod operator) It can be used with only integer values.

UNARY OPERATION Unary operators: The unary operator negates the value of its operand (clearly, a signed number). A numeric constant is assumed positive unless it is preceded by the negative operator. That is, there is no unary +. It is implicit. Remember that -x does not change the value of x at the location where it permanently resides in memory. Unary increment and decrement operators ++ and -- operators increment or decrement the value in a variable by 1. Basic rules for using ++ and operators: The operand must be a variable but not a constant or an expression. The operator ++ and -- may precede or succeed the operand.

POSTFIX The operator comes after the operand First use, then change Postfix: (a) x = a++; First action: store value of a in memory location for variable x. Second action: increment value of a by 1 and store result in memory location for variable a. (b) y = b ; First action: put value of b in memory location for variable y. Second action: decrement value of b by 1 and put result in memory location for variable b.

PREFIX The operator comes before the operand First change, then use Prefix : (a) x = ++a; First action: increment value of a by 1 and store result in memory location for variable a. Second action: store value of a in memory location for variable x. (b) y = b; First action: decrement value of b by 1 and put result in memory location for variable b. Second action: put value of b in memory location for variable y.

Example

Examples

CONDITIONAL OPERATOR/ TERNARY OPERATOR The conditional operator has three expressions. It has the general form expression1? expression2 : expression3 First, expression1 is evaluated; it is treated as a logical condition. If the result is true, then expression2 is evaluated and its value is the final result. Otherwise, expression3 is evaluated and its value is the final result. For example,int m = 1, n = 2, min; min = (m < n? m : n); /* min is assigned a value 1 */ In the above example, because m is less than n, m<n expression evaluates to be true, therefore, min is assigned the value m, i.e., 1.

RELATIONAL OPERATORS C provides six relational operators for comparing numeric quantities. Relational operators evaluate to 1, representing the true outcome, or 0, representing the false outcome.

LOGICAL OPERATORS C provides three logical operators for forming logical expressions. Like the relational operators, logical operators evaluate to 1 or 0. Logical negation is a unary operator that negates the logical value of its single operand. If its operand is non-zero, it produces 0, and if it is 0, it produces 1. Logical AND produces 0 if one or both its operands evaluate to 0. Otherwise, it produces 1. Logical OR produces 0 if both its operands evaluate to 0. Otherwise, it produces 1.

LOGICAL OPERATORS

LOGICAL OPERATORS All the expressions need not be evaluated when they are connected by && and

BIT WISE 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 sequences. Bitwise negation is a unary operator that complements the bits in its operands. Bitwise AND compares the corresponding bits of its operands and produces a 1 when both bits are 1, and 0 otherwise. Bitwise OR compares the corresponding bits of its operands and produces a 0 when both bits are 0, and 1 otherwise. Bitwise exclusive or compares the corresponding bits of its operands and produces a 0 when both bits are 1 or both bits are 0, and 1 otherwise.

BIT WISE OPERATORS

BIT WISE OPERATORS Bit 1 Bit 2 Bit1 & Bit2 Bit1 Bit2 Bit1 ^ Bit2 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0

COMMA OPERATOR This operator allows the evaluation of multiple expressions, separated by the comma, from left to right in order and the evaluated value of the rightmost expression is accepted as the final result. The general form of an expression using a comma operator is Expression M = (expression1, expression2,,expression N); where the expressions are evaluated strictly from left to right and their values discarded, except for the last one, whose type and value determine the result of the overall expression.

SIZEOF OPERATOR C provides a useful operator, sizeof, for calculating the size of any data item or type. It takes a single operand that may be a type name (e.g., int) or an expression (e.g.,100) and returns the size of the specified entity in bytes.the outcome is totally machine-dependent. For example:

EXPRESSION EVALUATION: PRECEDENCE & ASSOCIATIVITY Evaluation of an expression in C is very important to understand. Unfortunately there is no BODMAS rule in C language as found in algebra. The precedence of operators determines the order in which different operators are evaluated when they occur in the same expression. Operators of higher precedence are applied before operators of lower precedence.

EXAMPLE : OPERATOR PRECEDENCE

LVALUES AND RVALUES An lvalue is an expression to which a value can be assigned. An rvalue can be defined as an expression that can be assigned to an lvalue. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. The address associated with a program variable in C is called its lvalue; the contents of that location are its rvalue, the quantity that is supposed to be the value of the variable. The rvalue of a variable may change as program execution proceeds; but never its lvalue. The distinction between lvalues and rvalues becomes sharper if one considers the assignment operation with variables a and b.

LVALUES AND RVALUES For example : a = b; b, on the right-hand side of the assignment operator, is the quantity to be found at the address associated with b, i.e., an rvalue. a is assigned the value stored in the address associated with b. a, on the left-hand side, is the address at which the contents are altered as a result of the assignment. a is an lvalue. The assignment operation deposits b s rvalue at a s lvalue.

TYPE CONVERSION Though the C compiler performs automatic type conversions, the programmer should be aware of what is going on so as to understand how C evaluates expressions. When a C expression is evaluated, the resulting value has a particular data type. If all the variables in the expression are of the same type, the resulting type is of the same type as well. For example, if x and y are both of int type, the expression x +y is of int type as well. The smallest to the largest data types conversion with respect to size is along the arrow as shown below:

RULE:IMPLICIT TYPE CONVERSION float operands are converted to double. char or short (signed or unsigned) are converted to int (signed or unsigned). If any one operand is double, the other operand is also converted to double, and that is the type of the result; or If any one operand is long, the other operand is treated as long, and that is the type of the result; If any one operand is of type unsigned, the other operand is converted to unsigned, or the only remaining possibility is that Both operands must be int, and that is also the type of the result.

IMPLICIT TYPE CONVERSION

TYPE CONVERSION IN ASSIGNMENTS In certain cases, the type of the expression and the type of the variable on the left hand side of the assignment operator may not be same. In such case the value of the expression is promoted or demoted depending on the type of the variable on the left hand side

TYPE CONVERSION IN ASSIGNMENTS Consider the below given example

TYPE CASTING Syntax (type-name) expression The expression is temporarily converted to the type specified Example

Excercise WAP to accept p, c, m marks from the user and calculate the pcm total and the pcm% obtained.