What are Operators in C?
The C programming language makes use of operators as symbols representing exact operations to be executed on a number of operands. C supplies a variety of operators that may carry out arithmetic, logical, and bitwise operations and operations on pointers and arrays. Operators are symbols that assist in performing features of mathematical and logical nature. The classification of C operators is as follows:
- Arithmetic
- Relational
- Logical
- Bitwise
- Task
- Conditional
- Particular
Despite the fact that there are various operators, the execution of those operations occurs primarily based on the priority given to them. Priority is the order by which the compiler executes the code comprising quite a few operators.
Clarification of Operators in C
Beneath is an in depth rationalization of operators in C:
#1 Arithmetic Operators
These operators are answerable for performing arithmetic or mathematical operations like addition (+), subtraction (-), multiplication (*), division (/), the rest of the division (%), increment (++), and decrement (–).
There are two forms of arithmetic operators:
- Unary Operators: This sort of operator works with a single worth (operand) like ++ and –.
- Binary Operators: This sort of operator works with two operands like +,-,*,/
Here’s a tabular type of the variety of arithmetic operators in C with the features they carry out.
Operator | Perform |
+ | Provides two values |
– | Subtract a second worth from the primary. |
* | Multiply two values |
/ | Divide numerator by the denominator |
% | The rest of division |
++ | Increment operator – will increase integer worth by one. |
— | Decrement operator – decreases integer worth by one |
Instance: C Program utilizing arithmetic operators
#embody <stdio.h>
int essential()
{
int a = 12, b = 6, 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("The rest when a divided by b = %d n", c);
return 0;
}
Output:
#2 Relational Operators
After we need to examine the values of two operands, we use relational operators. If we need to examine that one operand is the same as or larger than different operands, we use the >= operator.
The beneath desk lists the relational operators in C with their features.
Operator | Perform | Instance |
== | It is going to examine if the 2 operands are equal | 6 == 2 returns 0 |
!= | It is going to examine if the 2 operands should not equal. | 6 != 2 returns 1 |
> | It is going to examine if the operand on the left is larger than the operand on the fitting | 6 > 2 returns 1 |
< | It is going to examine if the operand on the left is smaller than the fitting operand | 6 < 2 returns 0 |
>= | It is going to examine if the left operand is larger than or equal to the fitting operand | 6 >= 2 returns 1 |
<= | It is going to examine if the operand on the left is smaller than or equal to the fitting operand | 6 <= 2 return 0 |
Instance: C Program utilizing relational operators
#embody <stdio.h>
int essential()
{
int a = 7, b = 7, c = 10;
printf("%d == %d = %d n", a, b, a == b); // true
printf("%d == %d = %d n", a, c, a == c); // false
printf("%d > %d = %d n", a, b, a > b); //false
printf("%d > %d = %d n", a, c, a > c); //false
printf("%d < %d = %d n", a, b, a < b); //false
printf("%d < %d = %d n", a, c, a < c); //true
printf("%d != %d = %d n", a, b, a != b); //false
printf("%d != %d = %d n", a, c, a != c); //true
printf("%d >= %d = %d n", a, b, a >= b); //true
printf("%d >= %d = %d n", a, c, a >= c); //false
printf("%d <= %d = %d n", a, b, a <= b); //true
printf("%d <= %d = %d n", a, c, a <= c); //true
return 0;
}
Output:
#3 Logical Operators
Logical Operators are to get True or False outcomes.
The desk beneath lists the logical operators utilized in C
Operator | Perform | Instance (if a=1 and b=0) |
&& | Logical AND | (a && b) is fake |
|| | Logical OR | (a || b) is true |
! | Logical NOT | (!a) is fake |
Instance: C Program utilizing logical operators.
#embody <stdio.h>
int essential()
(c < b) equals to %d n", end result);
end result = (a != b)
Output:
#4 Bitwise Operators
These operators are for bit-level operations on the operands. The operators first convert into bit-level after which carry out the calculations.
Operator | Perform |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise unique OR |
~ | Bitwise complement |
<< | Shift left |
>> | Shift proper |
Instance: C program for Bitwise AND
#embody <stdio.h>
int essential()
{
int a = 10, b = 8;
printf("Output = %d", a&b);
return 0;
}
Output:
Clarification:
10 = 00001010 (In Binary)
8 = 00001000 (In Binary)
Bit Operation of 10 and eight
00001010 & 00001000 = 00001000 = 8 (In decimal)
#5 Task Operators
Most of these operators assist us assign a worth to a variable.
Operator | Perform | Instance |
= | It is going to assign values from right-side operands to left-side operands | a=b |
+= | It is going to add the fitting operand to the left operand and assign the end result to left | a+=b is identical as a=a+b |
-= | It is going to subtract the fitting operand from the left operand and assign the end result to the left operand | a-=b is identical as a=a-b |
*= | It is going to multiply the left operand with the fitting operand and assign the end result to the left operand | a*=b is identical as a=a*b |
/= | It is going to divide the left operand with the fitting operand and assign the end result to the left operand | a/=b is identical as a=a/b |
%= | It is going to calculate the modulus utilizing two operands and assign the end result to the left operand | a%=b is identical as a=apercentb |
#6 Conditional Operators
Also called Ternary Operator or? : Operator, these operators are helpful for decision-making.
Syntax:
Expression 1? Expression 2: Expression 3
Right here,? Represents the IF situation.
#7 Particular Operators
Listed below are some particular operators utilized in C
Operator | Perform |
& | This operator is used to get the tackle of the variable.
Instance: &a will give an tackle of a. |
* | This operator works as a pointer to a variable.
Instance: * a the place * is a pointer to the variable a. |
dimension of () | This operator provides the scale of the variable.
Instance: The dimensions of (char) will give us 1. |
Instance: C program utilizing a particular operator
#embody <stdio.h>
int essential()
{
int *ptr, q;
q = 40;
/* It assigns the tackle of q to ptr */
ptr = &q;
/* show q's worth utilizing ptr variable */
printf("%d", *ptr);
return 0;
}
Output:
C Operators Priority
Typically, arithmetic, logical, and relational operators are used whereas coding in C. The priority for these operators in arithmetic is larger than logical and relational. Observe that each one the operators in arithmetic additionally comply with a distinct order of priority. Let’s examine which operators maintain the very best priority.
Order of Priority in Arithmetic Operators
The increment and decrement (+ + and – -) operators maintain the very best priority in arithmetic operators. After that subsequent priority is for the unary minus ( – ) operator; subsequent, three operators, /, *, and %, have equal priority. The bottom priority is for the operators like addition ( + ) and subtraction ( – ). In case of equal precedence, the compiler takes cost whereas evaluating them. Keep in mind the C operator associativity rule, which is for all operators with the identical priority. Then the execution occurs from left to proper.
For instance,
#embody <stdio.h>
int essential() {
int a = 15, b = 20, c = 32, end result;
end result = a * b - ++c;
printf("The result's: %d", end result);
return 0;
}
Output:
Clarification: Right here, within the given equation, first, “++” executes; therefore the worth of c will probably be 33. Subsequent, “* “holds the very best priority after “++”. Therefore after the execution of “a * b,” the end result will probably be 300. Then the execution of “-” occurs and leads to 267.
Order of Priority in Relational/Logical Operators
The very best priority in relational/logical operators is logical, not (!). After that, larger than (>), larger than equal to (>=), lower than (<), and fewer than equal to (<=) operators maintain equal priority. Then “= =” and “ != ” operators maintain the equal priority. Subsequent comes the logical And (&&) operator. In the long run, logical OR (||) holds priority. Even for relational and logical operators, the execution of these operators who maintain the identical priority occurs by the compiler.
For instance,
10 > 1 + 8
Output: False
Clarification: Right here, we all know the arithmetic operator holds the very best priority. Therefore the execution of “1 + 8” occurs first. It leads to 9. Subsequent, evaluating the numbers occurs: “10 > 9”. So the ultimate end result will probably be False as 10 isn’t larger than 9.
Misc Operators in C
The Misc operators or miscellaneous operators are conditional operators that embody three operands. In these 3, the execution of the primary operand occurs first. Then the execution of the second operand, whether it is non-zero, or a 3rd operand executes to offer the mandatory Output. Apart from the operators mentioned above, C programming language helps a couple of different particular operators like sizeof and “?:”.
Operator | Description | Instance |
sizeof() | Finds the scale of a variable | sizeof(b), if b is an integer, then the Output will probably be 4. |
?: | Conditional operator | Situation? X: Y; right here, if the situation is true, the end result will probably be X, else Y. |
& | Tackle of a variable | &a, returns the precise tackle |
* | Pointer | *a |
Time and Area Complexity
Time and area complexity are the phrases in regards to the execution of an algorithm. The Time complexity is the time taken to run the algorithm as a operate of the enter. Area complexity is the area or reminiscence the algorithm takes as an enter operate. These two phrases rely upon many phrases like processor, working system, and so forth.
Closing Ideas
C operators are the symbols used to carry out relational, mathematical, bitwise, or logical operations. C language contains numerous operators to carry out numerous duties as crucial in this system. Totally different sorts of operators are arithmetic, logical, and relational.
Steadily Requested Questions (FAQS)
Q1. What are the boolean operators in C?
Reply: Boolean operators validate the connection between the operands by returning 0 (false) or 1 (true) as Output. There are three sorts of boolean operators AND (&&), OR (||), and NOT (!). When each the inputs are true, then the AND will probably be true. If one of many inputs is true, then OR will probably be true. NOT operator supplies the alternative worth of the enter.
Q2. What does ** imply in C?
Reply: The “**” in C is a double-pointer or pointer-to-pointer. The place * is a pointer that holds the tackle of the variable. ** imply the tackle of a variable already holding an tackle of a distinct variable.
Q3. What’s the distinction between prefix and postfix operators in C?
Reply: Prefix and postfix are the operators written earlier than and after the operands. These operators are the increment (+ +) and decrement (- -) operators. For instance, “++c” is the prefix operator, and “c++” is the postfix operator.
This autumn. What’s the Modulus operator?
Reply: The modulus operator is the arithmetic operator of C, and it really works between two operands. The division of the numerator worth by the denominator leads to the rest. In less complicated phrases, the produced relaxation for the integer division is the modulus operator.
Q5. Does C language help operator overloading?
Reply: Operator overloading is a technique of polymorphism the place the programmer makes particular modifications within the current code with out altering its which means. Operator overloading is feasible solely in C++. Since polymorphism is feasible solely in an object-oriented language, C doesn’t help operator overloading.
Really helpful Articles
This EDUCBA information to C Operators discusses the operators utilized in C language with their syntax and examples. EDUCBA additionally suggests the next articles to study extra.
- Comparability of C# vs JavaScript
- Record of C-Command
- Profession in C Programming
- Bitwise Operators in JavaScript
The put up C Operators appeared first on EDUCBA.