Вы находитесь на странице: 1из 17

Session 2

Linux, C, C++ / Programming with C / Session 2 / 1 of 17

Session Objectives
Explain Assignment Operator

Explain Arithmetic Operators


Understand Arithmetic Expressions

Understand Precedence of Operators


Explain Relational and Logical Operators Explain the Conditional Operators Explain the Comma Operators Explain C shorthand
Linux, C, C++ / Programming with C / Session 2 / 2 of 17

The Assignment Operator


In C, the assignment operator(=) can be used with any valid C expression.

Linux, C, C++ / Programming with C / Session 2 / 3 of 17

Introduction
C supports 4 types of operators -

Arithmetic

Logical

Relational

Bitwise

Linux, C, C++ / Programming with C / Session 2 / 4 of 17

Multiple Assignment
Many variables can be assigned the same value in a single statement.

However, you cannot do this :

Linux, C, C++ / Programming with C / Session 2 / 5 of 17

Arithmetic Operators
Arithmetic Operators are used to perform numerical operations They are divided into 2 classes :

Linux, C, C++ / Programming with C / Session 2 / 6 of 17

Binary Operators
The following example shows all the binary operators used in C

Linux, C, C++ / Programming with C / Session 2 / 7 of 17

Unary Operators

a = a + 1; can also be written as a++; a pls = +50 as a--; a= = -75; a - 1; can also be written

Linux, C, C++ / Programming with C / Session 2 / 8 of 17

Arithmetic Expressions
Mathematical expressions can be expressed in C using arithmetic operators

Examples

Linux, C, C++ / Programming with C / Session 2 / 9 of 17

Precedence Of Operators
Precedence establishes the hierarchy of one set of operators over another when an arithmetic expression is to be evaluated. It refers to the order in which C evaluates operators. The precedence of the operators can be altered by enclosing the expressions in parentheses

Linux, C, C++ / Programming with C / Session 2 / 10 of 17

Type Conversion
The automatic type conversions for evaluating an expression are given below -

For example,

Linux, C, C++ / Programming with C / Session 2 / 11 of 17

Relational & Logical Operators


Relational operators are used to test the relationship between two variables, or between a variable and a constant. They are used to find out the greater or smaller value between two variables, or compare two variables.

Linux, C, C++ / Programming with C / Session 2 / 12 of 17

Relational & Logical Operators


Logical operators are symbols that are used to combine or negate expressions containing relational operators

Linux, C, C++ / Programming with C / Session 2 / 13 of 17

The Conditional Operator


C offers a shorthand notation, which provides conditional execution of statements. The conditional operator has 2 parts; ? and :

The purpose of the above statement is to assign the value of num1 or num2, whichever is larger, to the variable max. First the condition (num1>num2) is evaluated. If it is true, the value num1 is assigned to max, else num2 is assigned to max

Linux, C, C++ / Programming with C / Session 2 / 14 of 17

The Comma Operator


The comma operator is used to string together several expressions. The expression on the right side becomes the value of the total comma-separated expression.

First b is assigned the value 5 and then a is assigned the value 7. The parentheses are necessary because the comma operator has a lower precedence than the assignment operator
Linux, C, C++ / Programming with C / Session 2 / 15 of 17

The sizeof operator


sizeof is a unary compile-time operator The use of the sizeof operator shall be clear from the following example -

Linux, C, C++ / Programming with C / Session 2 / 16 of 17

C Shorthand

Can be written as

Can be written as

Linux, C, C++ / Programming with C / Session 2 / 17 of 17

Вам также может понравиться