Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. C logical operators
In this articleThe logical operators perform logical-AND ( Syntax
RemarksLogical operators don't perform the usual arithmetic conversions. Instead, they evaluate each operand in terms of its equivalence to 0. The result of a
logical operation is either 0 or 1. The type of the result is The C logical operators are described below:
The operands of logical-AND and logical-OR expressions are evaluated from left to right. If the value of the first operand is sufficient to determine the result of the operation, the second operand isn't evaluated. This effect is known as "short-circuit" evaluation. There's a sequence point after the first operand in the expression. For more information, see Sequence points. ExamplesThe following examples illustrate the logical operators:
In this example, the
In this example, if See also
FeedbackSubmit and view feedback for Additional resourcesC 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 <stdio.h> 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.
Here is an example demonstrating the working of increment and decrement operator: // Examples of increment and decrement operators #include <stdio.h> 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; } Output:++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. Assignment Operator With ExampleAn 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:
The below example explains the working of assignment operator. // Examples of assignment operators #include <stdio.h> 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; } Output:b = 7 b = 14 b = 7 b = 49 b = 7 b = 0 Relational Operator With ExampleRelational 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.
Below is an example showing the working of the relational operator: // Example of relational operators #include <stdio.h> 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; } Output: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. Logical Operator With ExampleIn 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.
Following is the example that easily elaborates the working of the relational operator: // Working of logical operators #include <stdio.h> 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; } 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
Bitwise Operator With ExampleBitwise 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:
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.
Misc Operator With ExampleBesides 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 Precedence in COperator 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.
Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now! ConclusionIn 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. What does << mean in C?<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .
What is the symbol in C?Master C and Embedded C Programming- Learn as you go. Why is && used in C?The logical AND operator is represented as the '&&' double ampersand symbol. It checks the condition of two or more operands by combining in an expression, and if all the conditions are true, the logical AND operator returns the Boolean value true or 1. Else it returns false or 0.
What is () called in C?Function Calling:
It is called inside a program whenever it is required to call a function. It is only called by its name in the main() function of a program. We can pass the parameters to a function calling in the main() function. Syntax: Add(a, b) // a and b are the parameters.
|