- USER CORNER
- OVERVIEW | BASICS
- C DATA TYPES
- VARIABLES IN C
- CONSTANTS or LITERALS
- C OPERATORS & ENUMS
- DECISION MAKING
- LOOPING IN C
- CONTROL FLOW IN C
- C FUNCTIONS
- ARRAYS IN C
- POINTERS IN C
As studied earlier, Operators are the symbols, which performs operations on various data items known as operands. For Example: in a
b are operands and
+ is an operator.
Note:- To perform an operation, operators and operands are combined together forming an expression. For example, to perform an addition operation on operands a and b, the addition(+) operator is combined with the operands a and b forming an expression.
Depending on the function performed, the C operators can be classified into various categories.
Arithmetic operators are used to perform arithmetic calculations.
They can work on any built-in data type of C except on boolean type.
C provides various arithmetic operators that are,
|+||Adds two operands.||A + B = 30|
|−||Subtracts second operand from the first.||A − B = -10|
|*||Multiplies both operands.||A * B = 200|
|/||Divides numerator by de-numerator.||B / A = 2|
|%||Modulus Operator and remainder of after an integer division.||B % A = 0|
|++||Increment operator increases the integer value by one.||A++ = 11|
|--||Decrement operator decreases the integer value by one.||A-- = 9|
Addition is: 10
Relational operators ar eused to find out the relationship between two operands.
The various relational operators provided by C are less then '<', less than or equal to '<=', greater than '>', greater than or equal to '>=', equal to '==' and not equal to '!=' operators.
|==||Checks if the values of two operands are equal or not. If yes, then the condition becomes true.||(A == B) is not true.|
|!=||Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.||(A != B) is true.|
|>||Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.||(A > B) is not true.|
|<||Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true.||(A < B) is true.|
|>=||Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true.||(A >= B) is not true.|
|<=||Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.||(A <= B) is true.|
a is less than b c and d both are equal a and b are not equal
Logical operator is used to find out the relationship between two or more relationship expressions.
The C language logical operators allow a programmer to combine simple relational expression to form complex expressions by using logical not, and and or.
|&&||Logical AND operator. If both the operands are non-zero, then the condition becomes true.||(A && B) is false.|
|||||Logical OROperator. If any of the two operands is non-zero, then the condition becomes true.||(A || B) is true.|
|!||Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.||!(A && B) is true.|
Taking the logical And, when two expressions are added the resulting expression will be true only if both of the sub-expressions are true. Eg, (X>5) && (Y<15) will be only true if x greater than 5 and if y is less than 15. The second expression (Y,15) will not be evaluated if the first expression is false.
A logical expression is evaluated from left to right as far as needed to determine the logical result. Eg. in the expression (Y<15)&&(X++>15), the value of X is incremented only if the value of Y is less than 15.
The logical or is represented by two vertical bars between two other expression as expression1 || expression2. The logical result of the entire expression will be true if either of the two expression surrounding the || is logically true. Expression involving the logical or are evaluated from left to right only when the result of the logical expression is known.
Line 1 - Condition is true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true
Bitwise operators are used to perform bit-by-bit operations. This operator can be applied to all the primitive data types such as long, int, short, char and byte etc.
Various bitwise operators are bitwise AND(&), bitwise OR(!), bitwise exclusive OR(^) one's complement(~), shift left (<<), shift right(>>), shift right with zero fill (>>>).
The table given below lists the various bitwise operators.
|&||Bitwise AND Operator copies a bit to the result if it exists in both operands.||(A & B) = 12, i.e., 0000 1100|
||||Bitwise OR Operator copies a bit if it exists in either operand.||(A | B) = 61, i.e., 0011 1101|
|^||Bitwise XOR Operator copies the bit if it is set in one operand but not both.||(A ^ B) = 49, i.e., 0011 0001|
|~||Bitwise One's Complement Operator is unary and has the effect of 'flipping' bits.||(~A ) = ~(60), i.e,. -0111101|
|<<||Bitwise Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.||A << 2 = 240 i.e., 1111 0000|
|>>||Bitwise Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.||A >> 2 = 15 i.e., 0000 1111|
Line 1 - Value of c is 12 Line 2 - Value of c is 61 Line 3 - Value of c is 49 Line 4 - Value of c is -61 Line 5 - Value of c is 240 Line 6 - Value of c is 15
Assignment operators are used to assign the value of an expression to an identifier. The general form is,
v op =exp;
The most commonly used assignment operator is the equal sign (=). Assignment expressions are written in the form identifier = expression.
An assignment operator assumes there is a variable name to its left and an expression to its right.
The expression may be arithmetic, relational, logical or another assignment. It takes the value of the expression and stores it in the variable.
A=3 X=Y SUM =A+B
|=||assigns values from right side operands to left side operand||a=b|
|+=||adds right operand to the left operand and assign the result to left||a+=b is same as a=a+b|
|-=||subtracts right operand from the left operand and assign the result to left operand||a-=b is same as a=a-b|
|*=||mutiply left operand with the right operand and assign the result to left operand||a*=b is same as a=a*b|
|/=||divides left operand with the right operand and assign the result to left operand||a/=b is same as a=a/b|
|%=||calculate modulus using two operands and assign the result to left operand||a%=b is same as a=a%b|
|<<=||Left shift AND assignment operator.||C <<= 2 is same as C = C << 2|
|>>=||Right shift AND assignment operator.||C >>= 2 is same as C = C >> 2|
|&=||Bitwise AND assignment operator.||C &= 2 is same as C = C & 2|
|^=||Bitwise exclusive OR and assignment operator.||C ^= 2 is same as C = C ^ 2|
||=||Bitwise inclusive OR and assignment operator.||C |= 2 is same as C = C | 2|
The conditional operators are also called ternary operators. A conditional operators forms an expression that will have one of two values, depending on the truth value of another expression.
The expression can be written in place of an if-else statement. A conditional expression is written in the form expression1 ? expression2:expression3 When evaluating a conditional expression, expression1 is evaluated first.
If expression1 is tru(i.e., if its value is nonzero), then expression2 is evaluated and this becomes the value of the conditional expression.
Enter a signed numbe Absolute value = 22 Enter a signed number:23 Absolute value = 23
C has two very usefull operators.
They are increment(++) and decrement(--) operators. The increment operator(++) adds 1 to the operator value contained in the variable.
The decrement operator(--) subtracts 1 from the value contained in the variable.
Increment operator can be used in two way pre-increment(++a) and post-increment(a++).
Similarly decrement operator can also be used in two way pre-increment(--a) and post-increment(a--).
|A++||Add 1 to a variable agrer use.||int A=10,B;
|+AA||Add 1 to a variable before use.||int A=10,B;
|A--||Subtract 1 from a variable after use.||int A=10,B;
|--A||Subtract 1 from a variable before use.||int A=10,B;
10 10 11 10 11 11
In C programming the special operators are mostly used for memory related functions.
|&||This is used to get the address of the variable.
Example: &a will give address of a.
|*||This is used as pointer to a variable.
Example: *a where, * is pointer to the variable a.
|Sizeof ()||This gives the size of the variable.
Example: size of (char) will give us 1.
In this program, “&” symbol is used to get the address of the variable and “*” symbol is used to get the value of the variable that the pointer is pointing to. Please refer C – pointer topic to know more about pointers.
value is: 20