OPERATORS in C Programming

Slides:



Advertisements
Similar presentations
1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.
Advertisements

Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Types and Arithmetic Operators
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Objectives You should be able to describe: Data Types
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Operators & Expressions
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Operators and Expressions. Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==,
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Prepared By: K. U. Khimani Assistant Professor IT Department.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Fundamental Data Types and Storage Classes: A C language programmer has to tell the system before-hand, the type of numbers or characters he is using in.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CompSci 230 S Programming Techniques
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators And Expressions
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
INSPIRING CREATIVE AND INNOVATIVE MINDS
Computing Fundamentals
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Variable Declaration, Data types, Expressions
Precedence and Associativity
Variable Declarations, Data types, Expressions
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Basics of ‘C’.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
Introduction to Programming – 4 Operators
Chapter 2: Java Fundamentals
Chapter 3 Operators and Expressions
Herbert G. Mayer, PSU CS Status 7/19/2015
Chapter 4: Expression and Operator
C++ Programming Language Lecture 4 C++ Basics – Part II
OPERATORS AND EXPRESSIONS IN C++
Using C++ Arithmetic Operators and Control Structures
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Chapter 12 Variables and Operators
Presentation transcript:

OPERATORS in C Programming Dr. P. Lawrence Rozario Raj Assistant Professor, Department of Mathematics, St. Joseph’s College Trichirappalli – 620 002, Tamil Nadu, India. lawraj2006@yahoo.co.in

OPERATORS in C Programming C supports a rich set of operators OPERATORS in C Programming C supports a rich set of operators. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables.

C operators can be classical into. 1. Arithmetic operators. 2 C operators can be classical into 1. Arithmetic operators. 2. Relational operators. 3. Logical operators. 4. Assignment operators. 5. Increment operators. 6. Conditional operators. 7. Bitwise operators. 8. Special operators.

1. ARITHMETIC OPERATORS C provides all the basic arithmetic operators 1. ARITHMETIC OPERATORS C provides all the basic arithmetic operators. Operator Meaning   + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % Modulo division  

Integer division truncates any fractional part Integer division truncates any fractional part. The modulo division produces the remainder of an integer division. Integer Arithmetic When both the operands in a single arithmetic expression such as a+b are integers, the expression is called an integer expression, and the operation is called integer arithmetic. Integer arithmetic always yield an integer value. Examples of arithmetic operators are: a– b a + b a * b a / b a % b -a * b Here a and b are variables and are known as operands. The modulo division operator % cannot be used on floating point data.   a = 14 and b = 4 then a – b = 10 , a + b = 18, a * b = 56, a / b = 3 (decimal part truncated) , a % b = 2 (remainder of division)

Real Arithmetic An arithmetic operation involving only real operands is called real arithmetic. A real operand may assume values either in decimal or exponential notation. Since floating point values are rounded to the number of significant digits permissible, the final value is an approximation of the correct result. If x, y, and z are floats, then we will have; x = 6.0/7.0 = 0.857143 y = 1.0/3.0 = 0.333333 z = -2.0/3.0 = - 0.666667 the operator % cannot be used with real operands. Mixed-mode Arithmetic When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression. If either operand is of the real type, then only the real operation is performed and the result is always a real number. Thus 15/10.0 = 1.5 Where as 15/10 = 1  

2. RELATIONAL OPERATORS Comparisons can be done with the help of relational operators. Relational Operators Operator Meaning < is less than <= is less than or equal to > is greater than >= is greaten than or equal to = = is equal to ! = is not equal to  

A simple relational expression contains only one relational operator and takes the following from: ae–1 relational operator ae-2     ae-1 relational operator ae-2 ae-1 and ae-2 are arithmetic expressions. Which may be simple constants, variables or combination of them. Examples of simple relational expressions and their values: 4.5 <= 10 TRUE 4.5 < -10 FALSE When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will be evaluated first and then the results compared. That is, arithmetic operators have a higher priority over relational operators. Relational expressions are used in decision statements such as, if and while to etc.

3. LOGICAL OPERATORS C has the following three logical operators 3. LOGICAL OPERATORS C has the following three logical operators. & & meaning logical AND || meaning logical OR ! meaning logical NOT The logical operators && and are used when we want to test more than one condition and make decisions. Example : a > b && x = = 10 An expression of this kind which combines two or more relational expressions is termed as a logical expression or a compound relational expression.

The logical expression given above is true only if a>b is true and x = = 10 is true. If either (or both) of them are false, the expression is false. Truth Table   op-1 op-2   Non-zero Non-zero 1 1 Non-zero 0 0 1 0 Non-zero 0 1 0 0 0 0   Example: if (age > 55 && salary < 1000) if (number < 0 number > 100)

4. ASSIGNMENT OPERATORS Assignment operators are used to assign the result of an expression to a variable. We have seen the usual assignment operator, ‘ = ’. In addition, C has a set of ‘shorthand’ assignment operators of the form v op = exp    Where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator op = is known as the shorthand assignment operator. The assignment statement v op = exp; is equivalent to v = v op (exp); with v evaluated only once. Consider an example x + = y + 1 This is same as the statement x = x + (y + 1); The shorthand operator + = means ‘add y+1 to x’ or ‘increment x by y+1’. For y = 2, the above statement becomes x + = 3; and when this statement is executed, 3 is added to x. If the old value of x is, say 5, then the new value of x is 8.

Shorthand Assignment Operators. Statement with simple. Statement with Shorthand Assignment Operators Statement with simple Statement with Assignment operator shorthand operator   a = a + 1 a + = 1 a = a – 1 a - = 1 a = a * (n +1) a * = n+1 a = a / (n +1) a / = n + 1 a = a % b a % = b   The use of shorthand assignment operators has three advantages: 1 What appears on the left-hand side need not be repeated and therefore it becomes easier to write. 2. The statement is more concise and easier to read. 3 The statement is more efficient.

5. INCREMENT AND DECREMENT OPERATORS C has two very useful operators not generally found in other language. These are the increment and decrement operators: + + and - - The operator + + adds 1 to the operand while - - subtracts 1. Both are unary operators and take the following form: + +m; or m + +; - - m; or m - -; + +m; is equivalent to m = m + 1; (or m + = 1;) - - m; is equivalent to m = m – 1; (or m - = 1;) We use the increment and decrement statements in for and while loops extensively. While + + m and m + + mean the same thing when they from statements independently, they behave differently when they are used in expressions on the right-hand side of an assignment statement.

Consider the following:. m = 5; Consider the following: m = 5; y = + +m; In this case, the value of y and m would be 6. Suppose, if we rewrite the above statements as m = 5; y = m ++; then, the value of y would be 5 and m would be 6.   A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left. On the other hand, a postfix operator first assigns the value to the variable on left and then increments the operand.

6. CONDITIONAL OPERATOR A ternary operator pair “ 6. CONDITIONAL OPERATOR A ternary operator pair “?:” is available in C to construct conditional expressions of the form exp1? exp2 : exp3;   where exp1, exp2, and exp3 are expressions: The operator ? : works as follows : exp1 is evaluated first. If it is nonzero (true), then the expression exp2 is evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes the value of the expression. Note that only one of the expressions (either exp2 or exp3) is evaluated. Example : a = 10; b = 15; x = (a>b) ? a : b; then x = 15.

7 . BITWISE OPERATORS C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators may not be applied to float or double.   Bitwise Operators   Operator Meaning & bitwise AND | bitwise OR ^ Bitwise exclusive OR << shift left >> shift right ~ One’s complement  

8 . SPECIAL OPERATORS C supports some special operators of interest such as comma operator, sizeof operator, pointer operators (& and *) and member selection operators (.and ->) and preprocessor operators (# and ##). The Comma Operator The comma operator can be used to link the related expressions together. A comma-linked list of expressions are evaluated left to right and the value of right-most expression is the value of the combined expression. Example: value = (x = 10, y = 5, x+y); For (n=1, m = 10; n<=m; n ++, m ++)

The sizeof Operator The sizeof is a compile time operator and, when used eith an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. Examples: m = sizeof(sum); n = sizeof(long int); k = sizeof(235L); The sizeof operator is normally used to determine the lengths of arrays and structures when their sizes are not known to the programmer. It is also used to allocate memory space dynamically to variables during execution of a program.