Page 1 of 8
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
the division operator (/) requires that the second operand be nonzero Division of one integer quantity by another is referred to as integer division.0 respectively. together with their resulting values. the decimal portion of the quotient will be dropped. the result will be a floating-point quotient. respectively.‘C’-Language
Page 2 of 8
2. Similarly. This operation always results in a truncated quotient i.5 and 2. Expression
a+b a-b a*b a/b a%b
13 7 30 3 1
Ex: Now suppose that ‘v1’ and ‘v2’ are floating-point variables whose values are 12.e. They are
+ * / %
Addition Subtraction Multiplication Division Remainder after integer
The % operator is sometimes referred to as the modulus operator. together with their resulting values. or with one floating-point number and one integer. Several arithmetic expressions involving these variables are shown below. The remainder operator (%) requires that both operands be integers and the second operand be nonzero. They include:
1) 2) 3) 4) 5) 6) 7) 8) 1.Operators
Def: An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Bitwise operators Special operators
There are five arithmetic operators in C. Ex: Suppose that ‘a’ and ‘b’ are integer variables whose values are 10 and 3. if a division operation is carried out with tow floating-point numbers. C operators can be classified into a number of categories. Several arithmetic expressions involving these variables are shown below. Expression Value
. On the other hand. They usually form a part of the mathematical of logical expressions. Operators are used in programs to manipulate data and variables.Arithimetic Operators:
Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators.
< <= > >= == !=
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
. For example. The value of a relational expression is either true or false. For example 10 <20 true.* / 3. 1. but 20<10 false. Ex: 1) a+b*2/c+d*5 2) x/y*x*2/y*3 The following is the hierarchy of arithmetic operators.Operators
Def: Combination of some operands.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. take certain decisions.5 25. These comparisons can be done with the help of relational operators.( ) 2.0 6.+ % ----.‘C’-Language
v1+v2 v1-v2 v1*v2 v1/v2
Page 3 of 8
14. and so on. we may compare the age of two persons.5 10. and depending on their relation.25
2. constants with arithmetic operators is called an arithmetic expression. or the price of two items.
5 < -10 a+b = = c+d true false true
4) Logical Operators:
In addition to the relational operators. which may be simple constants. C has the following three logical operators. 4. 2.5 <= 10 4. Ex:
Page 4 of 8
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. 3. Truth Tables: && Exp1
T T F F
T F F F
T F T F
T T F F
T F T F
T T T F
Ex: 1. variables or combination of them. which combines two. a logical expression also yields a value of true or false. or more relational expression s is termed as a logical expression or a compound relational expression.(age>55 && salary<1000) 2.
Ex: (a>b) && (x==10)
An expression of this kind.(number<0 || number>100)
4) Assignment Operators:( =)
. Like the simple relational expressions.Operators
ae-1 and ae-2 are arithmetic expressions.
a variable or a more complex expression. The general form of this operation is Syn:
Variable++. The general form of compound assignment is
Variable = expression. generally not found in other programming languages. The ++ operator increments the value contained in a variable by one.
Where identifier generally represents a variable. they are ++ and --. delta=0. Ex: 1) 2) 3) 4) 5) a=3.001.
The forms of compound assignment operators and their corresponding expanded assignment statements are given below.‘C’-Language
Page 5 of 8
2. area = length * width
The right side expression to the assignment operator is evaluated first. C provides a compact and shorthand form of indicating this operation by compound assignment operator ‘+=’. sum=a+b. can be represented by x+=a. x=y. ------------------>post increment operator Variable--. The most commonly used assignment operator is =. and expression represents a constant. and that obtained resultant is stored into the left side variable to that assignment operator. Thus x=x+a.Operators
Assignment operators are used to assign the result of an expression to variable. ------------------>pre increment operator
Both are equivalent to Variable =Variable+1
+= -= *= /= %=
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.
Consider the following assignment statement x=x+a.
Page 6 of 8
If the expression1 being evaluated is true. the result will be b=6 a=6 a=b++.pre decrement
Both are equivalent to Variable =variable – 1.Operators
It is important to distinguish between pre-increment and post-increment operations. i. the result will be a=5 b=4
6) Conditional Operator (?:):
This is also known as ternary operator. x=10. It is called ternary operator because it uses three expressions. a=++b.
.-‘as a decrement operator. Ex: Int x. The format of – is Variable-. expression2 will be performed. y=5. first increment and do the operation.max. Ex: b=5. It is a short form of if-then-else statement. a=--b.post decrement
--------------. is equivalent to b=b+1 a=b.y.. On the other hand in the post increment operator first do the operation and then increment. is equivalent to a=b. The general from is:
Variable = Expression1? Expression1: Expression2. a=b++. expression3 will be performed. the result will be a=5 b=6 In a similar way. The following example gives the maximum of two numbers x andy. the result will be b=4 a=4 a=b--. b=b+1. a=++b. The pre-increment operator. If the expression is false.e. C uses ‘. --------------.
The meaning of the statement is if x is greater than y. The bitwise one’s complement operator (~) inverts each bit.‘C’-Language
Page 7 of 8
max=(x>y) ? x : y. otherwise maximum=y. is the conditional expression.
7) Bitwise Operators:
One byte is made up of 8 bits. Right shift operator Left shift operator
These operate on integers only and not on floating point types. A bit stores either 0 or 1. The bitwise XOR(^) operator returns 0 when both the operands are same and 1 when they are different.
2. 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. it The binary
Suppose the variable a and b have the values 7 and 12 respectively. In other words.Operators
Every thing between the = and. The following are the special operators in C. The operators provided for bit manipulation are:
Operato Meaning r
& | ^ ~ >> << bitwise AND bitwise OR bitwise exclusive OR bitwise one’s complement operator.
The bitwise OR operator(|) returns 1 if at least one of the operand is 1 and 0 when both are 0. converts each 1 bit to 0 and each 0 bit to a 1. The bitwise AND operator (&) returns 1 if both the operands are 1 and 0 otherwise. then maximum = x. 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.
Page 8 of 8 operation
2. The general form is (Type-desired) expression. here b=3 d=(float) a/(float)b = 0. b=(int ) c . c=3. Ex: 1.
.1415. .333 char ch. The operator used to force this conversion is called cast and the process is known as casting. a=1.
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.
It is possible to force an expression to be of specific type by using a construct called cast. Int x. X=(int) ch.‘C’-Language Operato r
& * sizeof .