0% found this document useful (0 votes)
86 views8 pages

Operators: Chapter-2

The document discusses various operators in C language classified into 8 categories: 1) Arithmetic operators 2) Relational operators 3) Logical operators 4) Assignment operators 5) Increment and decrement operators 6) Conditional operator 7) Bitwise operators 8) Special operators. It provides examples and explanations of each operator type along with their usage and precedence. Key points covered include the 5 basic arithmetic operators, relational operators for comparisons, logical operators for conditional testing, and usage of assignment, increment/decrement, conditional, and bitwise operators.

Uploaded by

spoosan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views8 pages

Operators: Chapter-2

The document discusses various operators in C language classified into 8 categories: 1) Arithmetic operators 2) Relational operators 3) Logical operators 4) Assignment operators 5) Increment and decrement operators 6) Conditional operator 7) Bitwise operators 8) Special operators. It provides examples and explanations of each operator type along with their usage and precedence. Key points covered include the 5 basic arithmetic operators, relational operators for comparisons, logical operators for conditional testing, and usage of assignment, increment/decrement, conditional, and bitwise operators.

Uploaded by

spoosan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

C-Language

Page 1 of 8

2.Operators

Chapter-2

Operators
1. Arithmetic operators 2. Relational operator 3. Logical operators 4. Assignment operator 5. Increment & decrement operators 6. Conditional operator 7. Bitwise operators 8. Special operators

C-Language

Page 2 of 8

2.Operators

Def: 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. They usually form a part of the mathematical of logical expressions. C operators can be classified into a number of categories. They include:

1) 2) 3) 4) 5) 6) 7) 8) 1.Arithimetic Operators:

Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators. Bitwise operators Special operators

There are five arithmetic operators in C. They are

Operator
+ * / %

Meaning
Addition Subtraction Multiplication Division Remainder after integer

The % operator is sometimes referred to as the modulus operator. The remainder operator (%) requires that both operands be integers and the second operand be nonzero. Similarly, the division operator (/) requires that the second operand be nonzero Division of one integer quantity by another is referred to as integer division. This operation always results in a truncated quotient i.e. the decimal portion of the quotient will be dropped. On the other hand, if a division operation is carried out with tow floating-point numbers, or with one floating-point number and one integer, the result will be a floating-point quotient. Ex: Suppose that a and b are integer variables whose values are 10 and 3, respectively. Several arithmetic expressions involving these variables are shown below, together with their resulting values. Expression
a+b a-b a*b a/b a%b

value
13 7 30 3 1

Ex: Now suppose that v1 and v2 are floating-point variables whose values are 12.5 and 2.0 respectively. Several arithmetic expressions involving these variables are shown below, together with their resulting values. Expression Value

C-Language
v1+v2 v1-v2 v1*v2 v1/v2

Page 3 of 8
14.5 10.5 25.0 6.25

2.Operators

Def: Combination of some operands, constants with arithmetic operators is called an arithmetic expression. Ex: 1) a+b*2/c+d*5 2) x/y*x*2/y*3 The following is the hierarchy of arithmetic operators. 1.( ) 2.* / 3.+ % ----- parenthesis

Ex: Determine the hierarchy of operations and evaluate the following expression:
I=2*3/4+4/4+8-2+5/8 I=6/4+4/4+8-2+5/8 I=1+4/4+8-2+5/8 I=1+1+8-2+5/8 I=1+1+8-2+0 I=2+8-2+0 I=10-2+0 I=8+0 I=8

2) Relational Operators:
We often compare two quantities, and depending on their relation, take certain decisions. For example, we may compare the age of two persons, or the price of two items, and so on. These comparisons can be done with the help of relational operators. The value of a relational expression is either true or false. For example 10 <20 true, but 20<10 false. Operato r
< <= > >= == !=

Meaning
is less than is less than or equal to is greater than is greater than or equal to is equal to is not equal to

A simple relational expression contains only one relational operator and takes the following form:

ae-1 relational operator ae-2

C-Language

Page 4 of 8

2.Operators

ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or combination of them. Ex:
1. 2. 3. 4.5 <= 10 4.5 < -10 a+b = = c+d true false true

4) Logical Operators:
In addition to the relational operators, C has the following three logical operators.

Operato Meaning r
&& || ! logical AND logical OR logical NOT

The logical operators && and || are used when we want to test more than one condition and make decisions.

Ex: (a>b) && (x==10)


An expression of this kind, which combines two, or more relational expression s is termed as a logical expression or a compound relational expression. Like the simple relational expressions, a logical expression also yields a value of true or false. Truth Tables: && Exp1
T T F F

|| Result
T F F F

Exp2
T F T F

Exp1
T T F F

Exp2
T F T F

Result
T T T F

! Expression
T F

Result
F T

Ex: 1.(age>55 && salary<1000) 2.(number<0 || number>100)

4) Assignment Operators:( =)

C-Language

Page 5 of 8

2.Operators

Assignment operators are used to assign the result of an expression to variable. The most commonly used assignment operator is =. Syn:

Variable = expression;
Where identifier generally represents a variable, and expression represents a constant, a variable or a more complex expression. Ex: 1) 2) 3) 4) 5) a=3; x=y; delta=0.001; sum=a+b; area = length * width

The right side expression to the assignment operator is evaluated first, and that obtained resultant is stored into the left side variable to that assignment operator.

Compound Assignment:
Consider the following assignment statement x=x+a; C provides a compact and shorthand form of indicating this operation by compound assignment operator +=. Thus x=x+a; can be represented by x+=a. The general form of compound assignment is

First-operand compound-assignment-operator

second-operand;

The forms of compound assignment operators and their corresponding expanded assignment statements are given below.

Operator
+= -= *= /= %=

exampl e
x+=a x-=a x*=a x/=a x%=a

equivalent assignment statement


x=x+a x=x-a x=x*a x=x/a x=x%a

5) Increment and Decrement operators:


These are very useful operators, generally not found in other programming languages, they are ++ and --. The ++ operator increments the value contained in a variable by one. The general form of this operation is Syn:

Variable++; ------------------>post increment operator Variable--; ------------------>pre increment operator


Both are equivalent to Variable =Variable+1

C-Language

Page 6 of 8

2.Operators

It is important to distinguish between pre-increment and post-increment operations. The pre-increment operator, first increment and do the operation. On the other hand in the post increment operator first do the operation and then increment. i.e. a=++b; is equivalent to b=b+1 a=b; a=b++; is equivalent to a=b; b=b+1;

Ex: b=5; a=++b; the result will be b=6 a=6 a=b++; the result will be a=5 b=6 In a similar way, C uses - -as a decrement operator. The format of is Variable-- ; --------------- post decrement

--Variable

--------------- pre decrement

Both are equivalent to Variable =variable 1; Ex: b=5; a=--b; the result will be b=4 a=4 a=b--; the result will be a=5 b=4

6) Conditional Operator (?:):


This is also known as ternary operator. It is called ternary operator because it uses three expressions. It is a short form of if-then-else statement. The general from is:

Variable = Expression1? Expression1: Expression2;


If the expression1 being evaluated is true, expression2 will be performed. If the expression is false, expression3 will be performed. The following example gives the maximum of two numbers x andy. Ex: Int x,y,max; x=10; y=5;

C-Language

Page 7 of 8
max=(x>y) ? x : y;

2.Operators

Every thing between the = and; is the conditional expression. The meaning of the statement is if x is greater than y, then maximum = x; otherwise maximum=y.

7) Bitwise Operators:
One byte is made up of 8 bits. A bit stores either 0 or 1. The C language provides facilities similar to assembly language to manipulate at the bit level of integers in addition to operations at the byte level. The operators provided for bit manipulation are:

Operato Meaning r
& | ^ ~ >> << bitwise AND bitwise OR bitwise exclusive OR bitwise ones complement operator. Right shift operator Left shift operator

These operate on integers only and not on floating point types. The bitwise AND operator (&) returns 1 if both the operands are 1 and 0 otherwise.

can

The bitwise OR operator(|) returns 1 if at least one of the operand is 1 and 0 when both are 0. The bitwise XOR(^) operator returns 0 when both the operands are same and 1 when they are different. The bitwise ones complement operator (~) inverts each bit. converts each 1 bit to 0 and each 0 bit to a 1. In other words, it The binary

Suppose the variable a and b have the values 7 and 12 respectively. representation of these variables are
A B A&B A|B A^B ~A ~B ----00000111 ----00001100 ----00000100 ----00001111 ----00001011 ----11111000 ----11110011

8) Special Operators:
There are special operators used in the C language to perform some particular type of operations. The following are the special operators in C.

C-Language Operato r
& * sizeof , . Comma Dot

Page 8 of 8 operation
Address Indirection

2.Operators

action performed
returns address of operand returns contents of location whose address is the operand returns size in bytes of the operand to separate elements or expressions in a list Member of structure or union pointer to a member of a structure or union.

Type Casting:
It is possible to force an expression to be of specific type by using a construct called cast. The operator used to force this conversion is called cast and the process is known as casting. The general form is (Type-desired) expression; Ex: 1. a=1; c=3.1415; b=(int ) c ; here b=3 d=(float) a/(float)b = 0.333 char ch; Int x; X=(int) ch; (double)(4*3/7)

Ex:

2.

Ex

3.

You might also like