UNIT 3 OPERATORS. [Marks- 12]

Similar documents
Operators and Expressions:

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

Operators in C. Staff Incharge: S.Sasirekha

Prepared by: Shraddha Modi

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.

Department of Computer Science

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

Unit 4. Input/Output Functions

Basics of Programming

Unit 3. Operators. School of Science and Technology INTRODUCTION

UNIT- 3 Introduction to C++

Operators & Expressions

Operators in java Operator operands.

Arithmetic Operators. Portability: Printing Numbers

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

Reserved Words and Identifiers

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 3: Operators, Expressions and Type Conversion

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Computers Programming Course 6. Iulian Năstac

Constants and Variables

Operators. Java operators are classified into three categories:

C - Basic Introduction

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

Computers Programming Course 5. Iulian Năstac

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

JAVA OPERATORS GENERAL

Expressions and Precedence. Last updated 12/10/18

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

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

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

Advantages of writing algorithm

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

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

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

Computer Programming CS F111

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

Programming in C++ 5. Integral data types

Operators And Expressions

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

Information Science 1

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

Chapter 4. Operations on Data

Infix to Postfix Conversion

UNIT IV INTRODUCTION TO C

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

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

DEPARTMENT OF MATHS, MJ COLLEGE

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

Department of Computer Applications

Expression and Operator

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

SECTION II: LANGUAGE BASICS

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

C Programming Multiple. Choice

More Programming Constructs -- Introduction

ISA 563 : Fundamentals of Systems Programming

Declaration and Memory

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

Bits, Words, and Integers

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

First of all, it is a variable, just like other variables you studied

Data Types. Data Types. Integer Types. Signed Integers

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

C Programming

Lecture 02 C FUNDAMENTALS

C: How to Program. Week /Mar/05

Programming for Engineers Introduction to C

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Prof. Navrati Saxena TA: Rochak Sachan

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Computer System and programming in C

These are reserved words of the C language. For example int, float, if, else, for, while etc.

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Continued from previous lecture

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

A First Program - Greeting.cpp

ME 461 C review Session Fall 2009 S. Keres

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

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

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

Chapter 2 - Introduction to C Programming

Fundamentals of Programming

UNIT-I Input/ Output functions and other library functions

Introduction to C++ Introduction and History. Characteristics of C++

Data types, variables, constants

Full file at

Structured Programming. Dr. Mohamed Khedr Lecture 4

Fundamental of C programming. - Ompal Singh

3. Types of Algorithmic and Program Instructions

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

Chapter 3 Structure of a C Program

Chapter 2: Using Data

Preview from Notesale.co.uk Page 6 of 52

Engineering Computing I

Transcription:

1 UNIT 3 OPERATORS [Marks- 12]

SYLLABUS 2

INTRODUCTION C supports a rich set of operators such as +, -, *,<,>,<=,etc... An operator is a some special characters (symbol) that tells the computer to perform certain mathematical or logical manipulations. 3

Categories of Operators: Arithmetic Operators Logical Operators Relational Operators Bitwise Operators Conditional Operators Assignment Operators Increment & Decrement Operators Special Operators 4

ARITHMETIC OPERATORS Operator Meaning + Addition - Subtraction * Multiplication / Devision % modulo Ex: a+b, a-b, a*b, a%b, a/b where a & b variables & known as Operand (variable). 5

Example: int a,b,sum,sub,div,mul,mod; a = 15; b = 3; sum = a+b; // sum = 18 sub = a-b; // sub = 12 mul = a*b; // mul = 45 div = a / b; // div = 5 mod = a%b; // mod = 0 6

RELATIONAL OPERATOR Compare two quantities & depending on their relation. Operator < Is less than Meaning <= Is less than or equal to > Is greater than >= Is greater than or equal to == Is equal to!= Is not equal to 7

Relational operators are used in decision statements. Such as while & if to decide the course of action of a running program. Example: int a = 15; int b = 12; Expression a>b a<b a>=b a<=b a==b a!=b Result True False True False False True 8

LOGICAL OPERATOR C has three logical operators. Operator && Meaning Logical AND Logical OR! Logical NOT The Logical operator && and are used when we want to test more than one condition and make decisions. 9

Example of Logical AND: int a=5; int b = 6; int c = 10; if(c>a && c>b) { printf( c is maximum ); } Output: c is maximum 10

Example of Logical OR: char ch; printf( Enter the character = ); scanf( %c,&ch); if(ch== y ch== Y ) { printf( You have entered the character y or Y ); } Output: Enter the character = y You have entered the character y or Y 11

ASSIGNMENT OPERATORS Used to assign the result of an expression to a variable. C has a set of shorthand assignment operators. Syntax: v op=exp; where, v is variable Example: x+=3; exp is an expression op is an arithmetic operator is equivalent to x=x+3; 12

Simple Assignment a=a+1 a=a-1 a=a*(n+1) a=a/(n+1) a=a%b Shorthand assignment a+=1 a-=1 a*=n+1 a/=n+1 a%=b Advantage: What appears on the left hand side need not be repeated. The statement is more concise & easier to read. The statement on more efficient. 13

INCREMENT & DECREMENT OPERATORS Increment Operator ++ Decrement operator -- The increment operator ++ adds 1 to the operand, while decrement operator -- subtracts 1. We use the increment and decrement statements in for and while loops. A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. A postfix operator first assigns the value to the variable on the left and then increment the operand. 14

POSTFIX INCREMENT OPERATOR Example: int a,b; a=10; b = a++; printf( a = %d,a); printf( b = %d,b); Output: a=11 b=10 15

PREFIX INCREMENT OPERATOR Example: int a,b; a=10; b = ++a; printf( a = %d,a); printf( b = %d,b); Output: a=11 b=11 16

POSTFIX DECREMENT OPERATOR Example: int a,b; a=10; b = a--; printf( a = %d,a); printf( b = %d,b); Output: a=9 b=10 17

PREFIX DECREMENT OPERATOR Example: int a,b; a=10; b = --a; printf( a = %d,a); printf( b = %d,b); Output: a=9 b=9 18

CONDITIONAL OPERATORS (TURNERY OPERATOR) Syntax: exp1? exp2:exp3 where, exp1,exp2 & exp3 are expressions. exp1 is evaluated first. If it is true then the expression exp2 is evaluated & becomes the value of an expression. If exp1 is false, then exp3 is evaluated and its value becomes the value of the expressions. Example: a=25; b=32; ans=(a>b)?a:b; In above example the answer is 35. 19

BITWISE OPERATOR Manipulation of data at bit level. Used for testing the bits, or shifting them right or left. Not to be applied to float or double. Operator & Meaning Bitwise AND Bitwise OR ^ Bitwise Exclusive OR << Shift left >> Shift right 20

BITWISE AND (&) The bitwise AND operator is represented by a single ampersand (&) and is surrounded on both sides by integer expressions. The result of ANDing operation is 1 if both the bits have a value of 1; otherwise it is 0. 21

Example: Let us consider two variables x and y whose values are 13 and 25. The binary representation of these two variables are x = 0000 0000 0000 1101 y = 0000 0000 0001 1001 If we execute statement z = x & y; then the result would be: z = 0000 0000 0000 1001 The resulting bit pattern represents the decimal number 9. 22

BITWISE OR( ) The bitwise OR operator is represented by vertical bar( ) and is surrounded on both sides by integer expressions. The result of OR operation is 1 if at least one of the bits has a value of 1;otherwise it is 0. 23

Example: Let us consider two variables x and y whose values are 13 and 25. The binary representation of these two variables are x = 0000 0000 0000 1101 y = 0000 0000 0001 1001 If we execute statement z = x y; then the result would be: z = 0000 0000 0001 1101 The resulting bit pattern represents the decimal number 29. 24

BITWISE EXCLUSIVE OR (^) The bitwise exclusive OR is represented by the symbol ^. The result of exclusive OR is 1 if only one of the bits is 1; otherwise it is 0. 25

Example: Let us consider two variables x and y whose values are 13 and 25. The binary representation of these two variables are x = 0000 0000 0000 1101 y = 0000 0000 0001 1001 If we execute statement z = x ^ y; then the result would be: z = 0000 0000 0001 0100 The resulting bit pattern represents the decimal number 20. 26

BITWISE SHIFT OPERATORS The shift operators are used to move bit patterns either left or to the right. The shift operators are represented by the symbols << and >> and are used in the following form: Left shift: op << n Right shift: op >> n where op is the integer expression that is to be shifted and n is the number of bit positions to be shifted. 27

BITWISE LEFT SHIFT OPERATOR The left shift operation causes all the bits in the operand op to be shifted to the left by n positions. The leftmost n bits in the original bit pattern will be lost and the rightmost n bit positions that are vacated will be filled with 0s. 28

Example: Suppose x is an unsigned integer whose bit pattern is then, 0000 0000 0010 1100 y = x << 3 = 0000 0001 0110 0000 Left shift operator is often used for multiplication by powers of two. In above example, The decimal value of y will be the value of x multiplied by 8. 29

BITWISE RIGHT SHIFT OPERATOR The right shift operation causes all the bits in the operand op to be shifted to the right by n positions. The rightmost n bits in the original bit pattern will be lost and the leftmost n bit positions that are vacated will be filled with 0s. 30

Example: Suppose x is an unsigned integer whose bit pattern is 0000 0000 0010 1100 then, y = x >> 3 = 0000 0000 0000 0101 Right shift operator is often used for division by powers of two. In above example, The decimal value of y will be the value of x divided by 8. 31

SPECIAL OPERATOR C supports some special operators such as comma operator, sizeof operator, pointer operator(& and *) and member selection operator(. and ->). 32

THE COMMA OPERATOR The comma operator is used to link the related expressions together. The comma-linked list of expressions are evaluated left to right and the value of right most expression is the value of the combined expressions. Example: value = (a=13, b=12, a+b); swap the value : t=x, x=y, y=t; 33

THE SIZEOF OPERATOR The sizeof is a compile time operator. When used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant, or a data type qualifier. Example: m=sizeof(sum); Use: 1) to determine the lengths of arrays and structures when their size are unknown to the programmer. 2) to allocate memory space dynamically to the variable. 34

Example: int a=10; int c; c = sizeof(a); printf( Size of a = %d,c); c = sizeof(float); printf( Size of float = %d,c); Output: Size of a = 2 Size of float = 4 35

EVALUATION OF EXPRESSIONS An arithmetic expressions without parenthesis will be evaluated from left to right using the rules of precedence of operator. There are two priority level of arithmetic operators: 1) High priority: * / % 2) Low priority: + - 36

The basic evaluation procedure include two leftto-right passes through the expressions. In the first pass, the high priority operators are applied as they are encountered. In second pass, the low priority operators are applied as they encountered. 37

Example: a = 5+15/3+4*6-9 First Pass: Step 1: a = 5+5+4*6-9 Step 2: a = 5+5+24-9 Second Pass: Step 1: a = 10+24-9 Step 2: a = 34-9 Step 3: a = 25 38

Whenever parenthesis are used, the expressions within parentheses assume highest priority. If two or more sets of parentheses appear one after another, then the expression contained in the left-most set is evaluated first and the rightmost in the last. 39

Example: a = 15+(12*2)-(23+2)/5 First Pass: Step 1: a = 15+24-(23+2)/5 Step 2: a = 15+24-25/5 Second Pass: Step 1: a = 15+24-5 Third Pass: Step 1: a = 39-5 Step 2: a = 34 40

TYPE CONVERSION In C we have two types of type conversion. (1) Implicit Type Conversion (2) Explicit Type conversion 41

IMPLICIT TYPE CONVERSION C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion. If the operands are of different types, then the lower type is automatically converted to the higher type before the operation proceeds. 42

EXPLICIT TYPE CONVERSION Example: 1) x = (int)7.5; then, 7.5 is converted to integer by truncation. 2) z = (int)a+b; then, a is converted to integer and after it added to b. 43

FORMATTED & UNFORMATTED INPUT/ OUTPUT IN C Reading data from input device and displaying the result on the screen are the two main tasks of any programming. In C language the input output operations are carried out by using standard functions. The programs for these functions are kept in the file. This file must be included in the program to use these functions using the pre processor directive as: #include The i/o functions are classified into 2 types : 1. formatted functions 2. Unformatted functions. 44

45

FORMATTED FUNCTIONS The formatted I/O functions read & write all types of data values. They require conversion symbol to identify the data type. scanf(_): This is formatted i/p function which can read data from the standard i/p device into the variable in different formats by the user. The function is used to read data more than one variable at a time. Syntax: scanf( control string, &arg); Example: scanf( %d,&no); 46

Printf(): Pritnf function is used to print the data (or) more than one data item in different formats on the standard o/p device. Syntax: printf( control string, arg1, arg2,.,argn); Control string consists of 3 types of items: 1. Characters that will be printed on the screen as they appear. 2. Format specifications that define the o/p format for display of each item. 3. Escape sequence characters such as \n, \t & \b. 47

Output of integer numbers: The format specification for printf on integer number is: %wd Where w specifies the minimum field width for the o/p d specifies that the value to be printed is an integer. The number is written right justified in the given field with: Ex: printf( %6d,9876); It is possible to force the printing to be left justified by placing a minus sign directly after the %character. Ex: printf( % 6d,9876); 9 8 7 6 9 8 7 6 48

It is also possible to pad with zero s the leading blanks by placing a 0 before the field width specifier. Ex: printf( %06d,9876); 0 0 9 8 7 6 The minus ( ) & zero are known as flags. Ex: printf( %d,9876); 9 8 7 6 49

Out put of real numbers: The output of a real no may be displayed in decimal notation using the following format specification %w.pf The integer w indication the minimum number of positions that are to be used for the display of the value and the integer p indicates the number of digits to be displayed after the decimal point (precision) f format specifies. We can also display a real no in exponential notation by using the specification. %w.pe 50

Example: y=98.76546 printf( %7.4f, y); printf( %7.2f,y); printf( %-7.2f, y); 9 8. 7 6 5 5 9 8. 7 7 9 8. 7 7 51

UNFORMATTED FUNCTIONS Unformatted Input Functions getchar() getch() gets() Unformatted Output Functions putchar() puts() 52

UNFORMATTED INPUT FUNCTIONS getchar( ) This function reads a character-type data from standard input. It reads one character at a time till the user presses the enter key. Syntax: Example: variable-name = getchar(); char c; c = getchar(); 53

getch( ) & getche( ) These functions read any alphanumeric character from the standard input device The character entered is not displayed by the getch() function until enter is pressed The getche() accepts and displays the character. The getch() accepts but does not display the character. Syntax & Example: getche( ); getch( ); 54

gets( ) This function is used for accepting any string until enter key is pressed. Syntax: gets(str); Example: char ch[30]; printf( Enter the string: ); gets(ch); printf( Entered string: %s, ch); Output Enter the string: Use of data! Entered string: Use of data! 55

UNFORMATTED OUTPUT FUNCTIONS putchar( ) This function prints one character on the screen at a time which is read by standard input. Syntax putchar( variable name); Example: char ch; printf( enter a character: ); scanf( %c, ch); putchar(ch); Output: enter a character: r r 56

puts( ) This function prints the string or character array. It is opposite to gets() Syntax puts(str); Example: char ch[30]; printf( Enter the string: ); gets(ch); puts( Entered string: ); puts(ch); Output: Enter the string: puts is in use Entered string: puts is in use 57

End 58