download the slides used in this presentation
PowerPoint pptx | Acrobat pdf
Objectives
While engaging with this module, you will...
- make comparisons between variables
- compound comparisons to make several evaluations at once
Equivalency
In C++, you will form logical expressions that evaluate to either true or false. In fact, true and false are reserved words in C++. There is a relationship between these words and the and numerical values. The compiler will interpret 0 as false and false as 0. It will interpret any other number with true, and true maps to 1 [may be different on different platforms]. Thus
- 0 false
- ≠0 -> true -> 1
As an example, if you were to output true to the screen, you get 1.
cout
It is used to check if the value of left operand is greater than the value of right operand. If the left operand is greater, then the condition becomes true.
[A > B] is not true.
It is used to check if the value of left operand is greater than or equal to the value of right operand. If the value of the left operand is greater than or equal to the value, then the condition becomes true.
[A >= B] is not true.
y];
printf["%d < %d is True [%d] \n", x, y, x < y];
printf["%d >= %d is False[%d] \n", x, y, x >= y];
printf["%d b] is %d \n", results];
results = [a == b] && [c < b];
printf["[a == b] && [c < b] is %d \n", results];
results = [a == b] || [c < b];
printf["[a == b] || [c < b] is %d \n", results];
results = [a != b] || [c < b];
printf["[a != b] || [c < b] is %d \n", results];
results = ![a != b];
printf["![a != b] is %d \n", results];
results = ![a == b];
printf["![a == b] is %d \n", results];
return 0;
}
Output:
[a == b] && [c > b] is 1
[a == b] && [c < b] is 0
[a == b] || [c < b] is 1
[a != b] || [c < b] is 0
![a != b] is 1
![a == b] is 0
- [a == b] && [c > 15] evaluates to 1 because both the operands [a == b] and [c > b] are 1 [true].
- [a == b] && [c < b] evaluates to 0 because of the operand [c < b] is 0 [false].
- [a == b] || [c < b] evaluates to 1 because of the operand [a = b] is 1 [true].
- [a != b] || [c < b] evaluates to 0 because both the operand [a != b] and [c < b] are 0 [false].
- ![a != b] evaluates to 1 because the operand [a != b] is 0 [false]. Hence, ![a != b] is 1 [true].
- ![a == b] evaluates to 0 because the [a == b] is 1 [true]. Hence, ![a == b] is 0 [false].
Bitwise Operator With Example
Bitwise operators are the operators which work on bits and perform the bit-by-bit operation. Mathematical operations like addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and easier to implement during computation and compiling of the program.
Bitwise operators are especially used in C programming for performing bit-level operations. C programming language supports a special operator for bit operation between two variables.
The truth tables for &, |, and ^ is provided below:
p | q | p & q | p | q | p ^ q |
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Here, we will assume that A = 50 and B = 25 in binary format as follows.
A = 00110010
B = 00011001
-----------------
A&B = 00010000
A|B = 00111011
A^B = 00101011
~A = 11001101
The table provided below demonstrates the bitwise operators supported by C. Assume variable 'A' holds 50 and variable 'B' holds 25.
Operator | Description | Example |
& | Binary AND Operator. It copies a bit to the result if it exists in both the operands. | [A & B] = 16, i.e. 00010000 |
| | Binary OR Operator. It copies a bit if and only if it exists in either operand. | [A | B] = 59, i.e. 00111011 |
^ | Binary XOR Operator. It copies the bit only if it is set in one operand but not both. | [A ^ B] = 43, i.e. 00101011 |
~ | Binary One's Complement Operator. It is unary and has the effect of 'flipping' bits. | [~A ] = ~[50], i.e,. -0111101 |
> 2 = 12 i.e., 00001100 |
Misc Operator With Example
Besides all the other operators discussed above, the C programming language also offers a few other important operators including sizeof, comma, pointer[*], and conditional operator [?:].
Operator | Description | Example |
sizeof[] | The sizeof is a unary operator that returns the size of data [constants, variables, array, structure, etc]. | sizeof[a], where a is integer, will return 4. |
& | It returns the address of a memory location of a variable. | &a; returns the actual address of the variable. |
* | Pointer to a variable. | *a; It points to the value of the variable. |
? : | conditional operator [?: in combination] to construct conditional expressions. | If Condition is true ? then value X : otherwise value Y will be returned as output. |
Operator Precedence in C
Operator precedence is also one of the features in the C programming language which helps to determine the grouping of terms in an expression and decides how an expression is evaluated as per the provided expressions. Some operators have higher precedence than others and some have lower precedence than others. For example, in C Language, the multiplication operator has higher precedence than the addition operator.
Example:
For expression x = 7 + 4 * 2 , x is assigned 15 and not 22 because Multiplication operator * has higher precedence than the addition operator +. So, it first multiplies 4 with 2 and then adds 7 into the expression.
Provided below is a table for better understanding of operator precedence. As we can see that the operators with the highest precedence appear at the top of the table and those with the lowest precedence appear at the bottom of the table. Within an expression in a C program, operators with higher precedence will be evaluated first and the operators with lower precedence will be evaluated later.
Category | Operator | Associativity |
Postfix | [] [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - [type]* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | > | Left to right |
Relational | < >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!
Conclusion
In this article on Operators in C, we have illustrated almost all the Operators in C with proper examples. The article starts with a brief introduction to Operators in C followed by elaborating the various types of Operators in C. We have provided a brief overview of all the Operators in C programming language and explained the basic introduction of the arithmetic operator, increment/decrement operator, assignment operator, relational operator, logical operator, bitwise operator, special operator, and also the operator precedence. After the overview, we have also illustrated the topic with an example for a better understanding of the topic. Some other important operators under the heading miscellaneous operators which are very useful in C programming have been discussed as well.
We hope through this article you could gain some knowledge on Operators in C and learned how we can use it in our software development projects.
To know more about the Operators in C, you can enroll in the Post-Graduate Program in Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp that includes 25 projects, a capstone project, and interactive online classes. In addition to the Operators in C and other related concepts, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.
Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.