Operators in C Topics to be covered q

Operators in C

Topics to be covered q. Operators, Operands & Expression q. Classification of Operators q. Arithmetic Operators q. Relational Operators q. Logical Operators q. Assignment Operators q. Increment/decrement Operators q. Conditional Operators q. Bitwise Operators q. Special Operators q. Precedence of operators

Operators, Operands & Expressions §Operators are symbols which take one or more operands or expressions and perform arithmetic or logical computations. §Operands are variables or expressions which are used in conjunction with operators to evaluate the expression. §Combination of operands and operators form an expression. §Expressions are sequences of operators, operands, and punctuators that specify a computation. Back

Classifications of Operators 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators Back

Arithmetic operators An expression that involves an arithmetic operator is known as arithmetic expression. Examples: 7+5=12 7 -5=2 7*5=35 7/5=2 7%5=2 Back

Relational Operators When we want to compare values then we use relational operator. Examples: (7>5)=T (7==5)=F (7<5)=F (7!=5)=T (7<=5)=F Back

Logical Operators Logical expression or a compound relational expression- An expression that combines two or more relational expressions Examples: if (a==b && b==c) Back

In Not operator values will be reverse like from 0 to 1 and vice-versa. Back

Assignment operators Assignment operator will be used to assign the result to variable Syntax: Var=exp; Example: a=5;

Shorthand assignment operators v op = exp; Where v = variable, op = shorthand assignment operator exp = expression Examples: x=x+3 y=y*3 x+=3 y*=3 Back

Increment & Decrement Operators C supports 2 useful operators namely Increment ++ Decrement –- operators The ++ operator adds a value 1 to the operand The –- operator subtracts 1 from the operand ++ and -- operator as prefix and postfix

Rules for ++ & -- operators 1. These require variables as their operands 2. When postfix either ++ or – is used with the variable in a given expression, the expression is evaluated first and then it is incremented or decremented by one 3. When prefix either ++ or – is used with the variable in a given expression, it is incremented or decremented by one first and then the expression is evaluated with the new value

++a or a++ --a or a– Suppose, a=5 then ++a; //a becomes 6 a++; //a becomes 7 --a; //a becomes 6 a--; //a becomes 5 Back

Conditional operators It is also known as ternary operator. Syntax: exp 1 ? exp 2 : exp 3 Where exp 1, exp 2 and exp 3 are expressions Working of the ? Operator: Expression 1 is evaluated first, if it is nonzero(1/true) then the expression 2 is evaluated and this becomes the value of the expression,

If exp 1 is false(0/zero) exp 3 is evaluated and its value becomes the value of the expression. Examples: m=2; n=3 r=(m>n) ? m : n; Back

Bitwise operators is used to manipulate the data at bit level.

Right Shift Ch=11100101 ch >> 1 01110010 Left Shift Ch=11100101 ch << 1 11001010 Back

Type Cast Operator Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another explicitly using the cast operator as follows: Syntax: (type_name) expression

Consider the following example where the cast operator causes the division of one integer variable by another to be performed as a floatingpoint operation: #include <stdio. h> void main() { int sum = 17, count = 5; double mean; clrscr(); mean = (double) sum / count; printf("Value of mean : %fn", mean ); getch(); } Back

Precedence of Operators C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied.

Back

Thanks
- Slides: 22