Operators
In c programming, an operator is a character that represents an action,
as for example * is an arithmetic operator that represents multiplication.
there are various types of operators in c language.they are as follows:
there are various types of operators in c language.they are as follows:
- C Arithmetic Operators
- C Relational Operators
- C Logical Operators
- C Bitwise Operators
- C Assignment Operators
[1]C Arithmetic Operators
Arithmetic operators perform the same basic operations you would expect if you used them in mathematics (with the exception of the percentage sign). They take two operands and return the result of the mathematical calculation.
C has five arithmetic operators:
Name Operator Example
Addition + num1 + num2
Subtraction - initial - spent
Multiplication * fathoms * 6
Division / sum / count
Modulus % m % n
pre increment ++ ++a
post increment ++ a++
pre increment ++ ++a
post increment ++ a++
pre decrement -- --a
post decrement -- a--
Note:--In c / (division operator) always gives quotient and %(modulus operator) always give the remainder of equation.
Increment Operator
• If we want to add one to a variable, we can
say:
count = count + 1 ;
• Programs often contain statements that
Increment variables, so to save on typing, C
provides these shortcuts:
count++ ; OR ++count ;
Both do the same thing. They change the
value of count by adding one to it but they
do this in slightly different manner.
Preincrement Operator
• If the ++ is before the variable(i.e ++a), then the
incrementing is done first (a preincrement).
int amount, count ;
count = 3 ;
amount = 2 * ++count ;
• 1 gets added to count first, then amount gets the
value of 2 * 4, which is 8.
• So, after executing the last line, amount is 8 and
count is 4.
Postincrement Operator
• The position of the ++ determines when the value
is incremented. If the ++ is after the variable, then
the incrementing is done last (a postincrement).
int amount, count ;
count = 3 ;
amount = 2 * count++ ;
• amount gets the value of 2 * 3, which is 6, and
then 1 gets added to count.
• So, after executing the last line, amount is 6 and
count is 4.
Decrement Operator
• If we want to subtract one from a variable, we
can say:
count = count - 1 ;
• Programs often contain statements that
decrement variables, so to save on typing, C
provides these shortcuts:
count-- ; OR --count ;
Both do the same thing. They change the
value of count by subtracting one from it.
Predecrement Operator
• If the -- is before the variable, then the
decrementing is done first (a predecrement).
int amount, count ;
count = 3 ;
amount = 2 * --count ;
• 1 gets subtracted from count first, then amount
gets the value of 2 * 2, which is 4.
• So, after executing the last line, amount is 4 and
count is 2.
Postdecrement Operator
• The position of the -- determines when the value is
Decremented. If the -- is after the variable, then
the decrementing is done last (a postdecrement).
int amount, count ;
count = 3 ;
amount = 2 * count-- ;
• amount gets the value of 2 * 3, which is 6, and
then 1 gets subtracted from count.
• So, after executing the last line, amount is 6 and
count is 2.
Given
int a = 1, b = 2, c = 3,D;
What is the value of this expression?
D= ++a * b - c--
What is the value of D?
Practice With Evaluating Expressions
Given integer variables a, b, c, d, and e,
where a = 1, b = 2, c = 3, d = 4,
evaluate the following expressions:
a + b - c + d
a * b / c
1 + a * b % c
a + d % b - c
e = b = d + c / b - a
[2]Relational Operator
A relational operator compares two operands to determine whether one is greater than, greater than or equal to, less than, less than or equal to the other.there are following conditional operators present in c:
> Greater than
>= greater than or equal to
< Less than
<= less than or equal to
When used in an expression they all return a Boolean value(true(non-zero) or false(zero)) which states the result of the comparison (i.e., 4 > 3 can be read as is 4 greater than 3? Which returns true).
Practice with Relational Expressions
Practice with Relational Expressions
Given
int a = 1, b = 2, c = 3 ;
Expression Result
a < c ??
a + b >= c ??
b <= c ??
a + b = = c ??
c <= a ??
a != b ??
a > b ??
a + b != c ??
b >= c ??
[3] Logical Operators
This is what logical operators can do for you. A logical operator combines one or two conditions into a single new condition. C provides three logical operators:
&& and.
|| or.
! not.
Let's look at an example. If you need to know if the value of the variable x is between zero and one, inclusive, you can write this with the help of logical operators for i.e:
if ((x >= 0.0) && (x <= 1.0))
what about or logical operator? the or logical operator is used where we have two conditions
and we only want one to fulfil.if you need to know the value of variable is vowel or not,you
can write this with the help of or logical operator
if((x=='a')||(x=='e')||(x=='i')||(x=='o')||(x=='u'))
The Operator ! is the C operator to perform the Boolean
operation NOT, it has only one operand, located at its right, and the
only thing that it does is to inverse the value of it, producing false
if its operand is true and true if its operand is false. Basically, it
returns the opposite Boolean value of evaluating its operand. For
example:
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true.
Practice With Logical Operators
Now you try. Translate the following English questions into C conditions.
1)Is the temperature temp greater than 32.0 and less than 212.0?
2)Is the height not equal to zero?
3)Is the absolute value of pos greater than 5.0?
[4] Bitwise Operators in C
Generally, as a programmer you don't need to concern yourself about operations at the bit level. You're free to think in bytes, or ints and doubles, or even higher level data types composed of a combination of these. But there are times when you'd like to be able to go to the level of an individual bit. Exclusive-or encryption is one example when you need bitwise operations.
& Bitwise And
| Bitwise OR
~ Bitwise Complement
^ Bitwise X-OR(exclusive-or)
| Bitwise OR
~ Bitwise Complement
^ Bitwise X-OR(exclusive-or)
Bitwise And
For instance, working with a byte (the char type):
01001000 & 10111000 = -------- 00001000 --------The most significant bit of the first number is 0, so we know the most significant bit of the result must be 0; in the second most significant bit, the bit of second number is zero, so we have the same result. The only time where both bits are 1, which is the only time the result will be 1, is the fifth bit from the left. Consequently,
72 & 184 = 8Bitwise OR
Bitwise OR works almost exactly the same way as bitwise AND. The only difference is that only one of the two bits needs to be a 1 for that position's bit in the result to be 1. (If both bits are a 1, the result will also have a 1 in that position.) The symbol is a pipe: |. Again, this is similar to boolean logical operator, which is ||.
01001000 | 10111000 = -------- 11111000 --------and consequently
72 | 184 = 248
Bitwise Complement
The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.
unsigned int max = ~0;
unsigned int max = ~0;
of course, is all 0s: 00000000 00000000. Once we twiddle 0, we get all 1s: 11111111 11111111.
Bitwise Exclusive OR(X-OR)
There is no boolean operator counterpart to bitwise exclusive-or, but there is a simple explanation. The exclusive-or operation takes two inputs and returns a 1 if either one or the other of the inputs is a 1, but not if both are. That is, if both inputs are 1 or both inputs are 0, it returns 0. Bitwise exclusive-or, with the operator of a carrot, ^, performs the exclusive-or operation on each pair of bits. Exclusive-or is commonly abbreviated XOR.
For instance, if you have two numbers represented in binary as 10101010 and 01110010 then taking the bitwise XOR results in 11011000. It's easier to see this if the bits are lined up correctly:
01110010 ^ 10101010 ------------ 11011000
----------
You can think of XOR in the following way: you have some bit, either 1 or 0, that we'll call A. When you take A XOR 0, then you always get A back: if A is 1, you get 1, and if A is 0, you get 0. On the other hand, when you take A XOR 1, you flip A. If A is 0, you get 1; if A is 1, you get 0.
[5] Assignment Operators in C
An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but is not an l-value.In C any value that is having an address is called an Lvalue.there are following versions of assignment operator avaliable in c.In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place.
An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but is not an l-value.In C any value that is having an address is called an Lvalue.there are following versions of assignment operator avaliable in c.In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place.
| |||||||||||||||||||||||
Practice with Assignment Operators Given int i = 1, j = 2, k = 3, m = 4,Z ; Expression Value Z+= j + k Z*= k = m + 5 Z-= m /= j * 2 find out the value of Z? | |||||||||||||||||||||||
Operator Precedence and Associativity | |||||||||||||||||||||||
No comments:
Post a Comment