What role do the relational operators play How is relational operator == differs from =?
Show
download the slides used in this presentation ObjectivesWhile engaging with this module, you will...
EquivalencyIn 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
As an example, if you were to output true to the screen, you get 1. cout< The relational operators are �<�, �<=�, �>�, �>=�, �==�, �!=�. The first 4 are clear and not much need be said about them. For example... short val = 5, num = 8, bob = 0; The == operator is the �is equal� operator and �!=� is the �is not equal� operator. (val == num); // true I now bring special attention to the == operator. Many times, those learning C++ for the first time will make a mistake when trying to use this operator that the compiler will NOT catch. The code will compile and run, but incorrectly! They will use
the = operator instead of the == operator. Thus, val = num will compile and run but will NOT compare the two values. It will set the value of the variable val to that of num and will return true...always. This is hardly the desired result. BE CAREFUL. The logical operators are �&&�, �||�, and �!�. Note: and (&&) has a higher precedence than or (||). You should check the order of precedence of the operators in the appendix in
the text. If you are unfamiliar with basic logic, this is how it works: a && b is true only if they are both true; a || b is false only if they are both false. !a is not a; i.e. the �!� operator changes true to false and vice versa. (val == num) || ( ! val); C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program. Usually, operators take part in a
program for manipulating data and variables and form a part of the mathematical, conditional, or logical expressions. In other words, we can also say that an operator is a symbol that tells the compiler to perform specific mathematical, conditional, or logical
functions. It is a symbol that operates on a value or a variable. For example, + and - are the operators to perform addition and subtraction in any C program. C has many operators that almost perform all types of operations.
These operators are really useful and can be used to perform every operation. Additionally, you can also learn more about the uses of C language. Arithmetic Operators are the operators which are used to perform mathematical calculations like addition (+),
subtraction (-), multiplication (*), division (/), and modulus (%). It performs all the operations on numerical values (constants and variables). The following table provided below shows all the arithmetic operators supported by the C language for performing arithmetic operators. It adds two operands − (Subtraction) It subtracts second operand from the first * (Multiplication) It multiplies both operands / (Division) It is responsible for dividing numerator by the denomerator % (Modulus) This operator gives the remainder of an integer after division Let’s look at an example of arithmetic operations in C below assuming variable a holds 7 and variable b holds 5. // Examples of arithmetic operators in C #include int main() { int a = 7,b = 5, c; c = a+b; printf("a+b = %d \n",c); c = a-b; printf("a-b =
%d \n",c); c = a*b; printf("a*b = %d \n",c); c = a/b; printf("a/b = %d \n",c); c = a%b; printf("Remainder when a is divided by b = %d \n",c); return 0; } a+b = 12 a-b = 2 a*b = 35 a/b = 1 Remainder when a divided
by b = 2 The operators shown in the program are +, -, and * that computes addition, subtraction, and multiplication respectively. In normal calculation, 7/5 = 1.4. However, the output is 1 in the above program. The reason behind this is that both the variables a and b are integers. Hence, the output should also be an integer. So, the compiler neglects the term after the decimal point and shows 2 instead of 2.25 as the output of the program. A modulo operator can only be used
with integers. Using modulo operator (%), you can compute the remainder of any integer. When a=7 is divided by b=5, the remainder is 2. If we want the result of our division operator in decimal values, then either one of the operands should be a floating-point number. Suppose a = 7.0, b = 2.0, c = 5, and d = 3, the output will be: // When either one of the operands is a floating-point number a/b = 3.50 a/d = 2.33 c/b =
1.66 // when both operands are integers c/d = 1 C programming has basically two operators which can increment ++ and decrement -- the value of a variable. It can change the value of an operand (constant or variable) by 1. Increment and Decrement Operators are very useful operators that are generally used to minimize the calculation. These
two operators are unary operators, which means they can only operate on a single operand. For example, ++x and x++ means x=x+1 or --x and x−− means x=x-1. There is a slight distinction between ++ or −− when written before or after any operand. If we use the operator as a pre-fix, it adds 1 to the operand, and the result is assigned to the variable on the left. Whereas, when it is used as a post-fix, it first assigns the value to the variable on the left i.e., it first
returns the original value, and then the operand is incremented by 1. ++ This increment operator increases the integer value by 1. -- This decrement operator decreases the integer value by 1. Here is an example demonstrating the working of increment and decrement operator: // Examples of increment and decrement operators #include int main() { int a = 11, b = 90; float c = 100.5, d = 10.5; printf("++a = %d \n", ++a); printf("--b = %d \n", --b); printf("++c = %f
\n", ++c); printf("--d = %f \n", --d); return 0; } ++a = 12 --b = 89 ++c = 101.500000 --d = 9.500000 In the above code example, the increment and decrement operators ++ and -- have been used as prefixes. Note that these two operators can also be used as postfixes like a++ and a-- when required. An assignment operator is mainly responsible for assigning a value to a variable in a program. Assignment operators are applied to assign the result of an expression to a variable. This operator plays a crucial role in assigning the values to any variable. The most common assignment operator is =. C language has a collection of shorthand assignment operators that can be used for C programming. The table below lists all the assignment operators supported
by the C language: = Assign Used to assign the values from right side of the operands to left side of the operand. C = A + B will assign the value of A + B to C. += Add then assign Adds the value of the right operand to the value of the left operand and assigns the result to the left operand. C += A is same as C = C + A -= Subtract then assign Subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand. C -= A is same as C = C - A *= Multiply then assign Multiplies the value of the right operand with the value of the left operand and assigns the result to the left operand. C *= A is same as C = C * A /= Divide then assign Divides the value of the left operand with the value of the right operand and assigns the result to the left operand. C /= A is same as C = C / A %= Modulus then assign Takes modulus using the values of the two operands and assigns the result to the left operand. C %= A is same as C = C % A <<= Left shift and assign Used for left shift AND assignment operator. C <<= 4 is same as C = C << 4 >>= Right shift and assign Used for right shift AND assignment operator. C >>= 5 is same as C = C >> 5 &= Bitwise AND assign Used for bitwise AND assignment operator. C &= 7 is same as C = C & 7 ^= Used for bitwise exclusive OR and assignment operator. C ^= 6 is same as C = C ^ 6 |= Used for bitwise inclusive OR and assignment operator. C |= 9 is same as C = C | 9 The below example explains the working of assignment operator. // Examples of assignment operators #include int main() { int a = 7, b; b = a; // b is 7 printf("b = %d\n", b); b += a; // b is 14 printf("b = %d\n", b); b
-= a; // b is 7 printf("b = %d\n", b); b *= a; // b is 49 printf("b = %d\n", b); b /= a; // b is 7 printf("c = %d\n", c); b %= a; // b = 0 printf("b = %d\n", b); return 0; } b = 7 b = 14 b = 7 b = 49 b = 7 b = 0 Relational operators are specifically used to compare two quantities or values in a program. It checks the relationship between two operands. If the given relation is true, it will return 1 and if the relation is false, then it will return 0. Relational operators are heavily used in decision-making
and performing loop operations. The table below shows all the relational operators supported by C. Here, we assume that the variable A holds 15 and the variable B holds the 25. == It is used to check if the values of the two operands are equal or not. If the values of the two operands are equal, then the condition becomes true. (A == B) is not true. != It is used to check if the values of the two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true. > 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 less than the value of right operand. If the left operand is lesser, then the condition becomes true. (A < B) is 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. <= It is used to check if the value of left operand is less than or equal to the value of right operand. If the value of the left operand is less than or equal to the value, then the condition becomes true. (A <= B) is true. Below is an example showing the working of the relational operator: // Example of relational operators #include int main() { int x = 8, y = 10; printf("%d == %d is False(%d) \n", x, y, x == 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
< %d is True (%d) \n", x, y, x < y); printf("%d >= %d is False(%d) \n", x, y, x >= y); printf("%d <= %d is True(%d) \n", x, y, x <= y); return 0; } 8 == 10 is False(0) 8 != 10 is True(1) 8 > 10 is False(0) 8 < 10 is True(1) 8 >= 10 is False(0) 8 <=10 is True(1) All the relational operators work in the
same manner as described in the table above. In the C programming language, we have three logical operators when we need to test more than one condition to make decisions. These logical operators are: An expression
containing a logical operator in C language returns either 0 or 1 depending upon the condition whether the expression results in true or false. Logical operators are generally used for decision-making in C programming. The table below shows all the logical operators supported by the C programming language. We are here assuming that the variable A holds 7 and variable B holds 3. && This is the AND operator in C programming language. It performs logical conjunction of two expressions. (If both expressions evaluate to True, then the result is True. If either of the expression evaluates to False, then the result is False) ((A==7) && (B>7)) equals to 0 || It is the NOT operator in C programming language. It performs a logical disjunction on two expressions. (If either or both of the expressions evaluate to True, then the result is True) ((A==7) || (B>7)) equals to 1 ! It is the Logical NOT Operator in C programming language. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false and vice versa. !(A && B) is true Following is the example that easily elaborates the working of the relational operator: // Working of logical operators #include int main() { int a = 15, b = 15, c = 20, 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) || (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; } (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 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. & 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 << Binary Left Shift Operator. The value of the left operands is moved left by the number of bits specified by the right operand. A << 2 = 200 i.e. 11001000 >> Binary Right Shift Operator. The value of the left operands is moved right by the number of bits specified by the right operand. A >> 2 = 12 i.e., 00001100 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 (?:). 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 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. 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. 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 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. |