C Operators: The Complete Guide for Beginners 2021

C Operators

A symbol that operates on a value or variable is known as an C operators. C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions.(from wikipedia )

With the help of examples, you will learn about several operators in C programming in this course. For example, the operator + is used to execute addition. C contains a large number of operators that can be used to do a variety of tasks.

The C language has a large number of built-in operators, including the following:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc Operators

Operators in C for Arithmetic

On numerical values, an arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, and division (constants and variables).

The table below lists all of the arithmetic operators offered by the C programming language. Assume variable X has a value of 20 and variable Y has a value of 45.

OperatorMeaning of OperatorDescriptionExample
+addition or unary plusAdds two operands to the equation.X + Y= 65
subtraction or unary minusThe second operand is subtracted from the first.X – Y= -25
*multiplicationBoth operands are multiplied.X * Y= 900
/divisionNumerator is divided by de-numerator.Y / X = 2
%remainder after division (modulo division)After an integer division, the remainder of the modulus operator.Y % X = 5
++Increases Increment The increment operator adds one to the integer value.X++ = 21
Decreases Increment  The increment operator reduces the value of an integer by one.Y– = 44

Example of C Arithmetic Operators

As you might guess, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.

45/20 Equals 2.25 in conventional math. In the program, however, the output is 2. Because both x and y are integers, this is the case. As a result, the result is also an integer. The compiler ignores the phrase after the decimal point, resulting in an answer of 2 rather than 2.25.

The residual is computed using the percent modulo operator. The remaining is 5 when x=20 is divided by y=45. Only integers can be used with the percent operator.

Output:

C Increment and Decrement Operators

The increment ++ and decrement operators in C programming modify the value of an operand (constant or variable) by one.

Example of C Increment and Decrement Operators

Decrement — reduces the value by one, whereas increment ++ increases it by one. These two operators are unary, which means they only work with one operand.

Output:

C Relational Operators

The relationship between two operands is checked by a relational operator. It yields 1 if the relationship is true, and 0 if the relationship is false.

The table below lists all of the relational operators that C supports. Assume variable A has a value of 10 and variable B has a value of 20.

OperatorDescriptionExample
==Checks whether two operands have the same value. If the answer is yes, the condition is true.(A == B) is not true.
!=Checks whether two operands have the same value. The condition becomes true if the values are not equal.(A != B) is true.
>Checks whether the left operand’s value is greater than the right operand’s value. If the answer is yes, the condition is true.(A > B) is not true.
<Checks whether the left operand’s value is less than the right operand’s value. If the answer is yes, the condition is true.(A < B) is true.
>=Checks whether the left operand’s value is greater than or equal to the right operand’s value. If the answer is yes, the condition is true.(A >= B) is not true.
<=Checks whether the left operand’s value is less than or equal to the right operand’s value. If the answer is yes, the condition is true.(A <= B) is true.

Example of C Relational Operators

We verified X and Y values against every relational operator we have in this relational operators example. 1 is TRUE, while 0 is FALSE.

Output:

C Logical Operators

Depending on whether the expression is true or false, a logical operator expression returns either 0 or 1. In C programming, logical operators are frequently employed in decision-making.

OperatorMeaningExample
&&It’s known as the logical AND operator. The condition is true if both operands are non-zero.Expression ((c==5) && (d>5)) equals 0 if c = 5 and d = 2.
||The Logical OR Operator is a term used to describe a type of logic. The condition becomes true if any of the two operands is non-zero.Expression ((c==5) || (d>5)) equals 1 if c = 5 and d = 2.
!It’s known as the Logical NOT Operator. Its operand’s logical state is reversed using it. If a condition is true, the Logical NOT operator will turn it into a false condition.When c equals 5, the expression!(c==5) equals 0.

Example of Logical Operators

  • Because both operands (a == b) and (c > b) are 1, (a == b) && (c > 5) evaluates to 1. (true).
  • Because operand (c b) is 0, (a == b) && (c b) evaluates to 0. (false).
  • (a == b) || (c b) equals 1 since (a = b) equals 1. (true).
  • Because both operands (a!= b) and (c b) are 0, (a!= b) || (c b) evaluates to 0. (false).
  • Because operand (a!= b) is 0,!(a!= b) evaluates to 1. (false). As a result,!(a!= b) equals 1. (true).
  • !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

Output:

C Bitwise Operators

This lesson will teach you how to use all six bitwise operators in C programming, along with examples.

Mathematical operations such as addition, subtraction, multiplication, and division are performed in bit-level in the arithmetic-logic unit (which is part of the CPU). Bitwise operators are used in C programming to execute bit-level operations.

In binary representation, assuming A = 60 and B = 13, they will be as follows:

  • A = 0011 1100
  • B = 0000 1101

—————–

  • A&B = 0000 1100
  • A|B = 0011 1101
  • A^B = 0011 0001
  • ~A = 1100 0011

The bitwise operators supported by C are listed in the table below. Assume variable ‘A’ has a value of 60 and variable ‘B’ has a value of 13, then

OperatorDescriptionExample
&If a bit exists in both operands, the binary AND operator duplicates it to the result.(A & B) = 12, example : 0000 1100
|If a bit exists in both operands, the binary OR operator copies it.(A | B) = 61, example : 0011 1101
^If a bit is set in one operand but not both, the binary XOR operator replicates it.(A ^ B) = 49, example : 0011 0001
~The Binary One’s Complement Operator is a unary operation that ‘flips’ bits.(~A ) = ~(60), example : -0111101
<<Left Shift Operator in Binary. The value of the left operand is shifted to the left by the number of bits given by the right operand.A << 2 = 240, example : 1111 0000
>>Right Shift Operator in Binary. The value of the left operand is advanced by the number of bits supplied by the right operand.A >> 2 = 15, example : 0000 1111

Bitwise AND operator &

If the matching bits of two operands are 1, the outcome of bitwise AND is 1. If one of an operand’s bits is 0, the result of the associated bit is also 0.

Consider the bitwise AND operation between two integers of 12 and 25.

Example of Bitwise AND

We declared two integers, x and y, and assigned the values 9 and 25 to them in our bitwise operators program. 12= 0001001 and 25= 00011001 in binary. The printf statements below will conduct bitwise operations on a and b, then display the results.

Output:

Bitwise OR operator |

When at least one corresponding bit of two operands is 1, the output of bitwise OR is 1. The bitwise OR operator is indicated by the symbol | in C programming.

Example of Bitwise OR

We declared two integers, x and y, and gave them the values 12 and 25 in our bitwise operators program. 12= 0001001 and 25= 00011001 are the binary equivalents of each other. The printf commands that follow will execute bitwise operations on a and b before printing the results.

Output:

Bitwise XOR (exclusive OR) operator ^

If the corresponding bits of two operands are opposite, the outcome of the bitwise XOR operator is 1. It is denoted by the symbol ^.

Example of Bitwise XOR

We declared two integers, x and y, and assigned them the values 9 and 65 in our bitwise operators program. 12= 0001001 and 12= 0001001 and 25= 00011001 in binary form. The printf commands below will execute bitwise operations on x and y before displaying the results.

Output:

Bitwise complement operator ~

The unary complement operator is a bitwise complement operator (works on only one operand). It transforms 1 into 0 and 0 into 1. It is denoted by the symbol.

Example of Bitwise complement

We declared two output, and assigned them the values 35 and 12 in our bitwise operators program. 35 = 00100011 in binary form. The printf commands below will execute bitwise operations on x and y before displaying the results.

Output:

Shift Operators in C

In C programming, there are two shift operators:

Operator on the right shift
The left shift Operator
Operator on the Right Shift
The right shift operator shifts all bits to the right by a specified amount of bits. >> is the symbol for it.

Left Shift Operator in C

The left shift operator moves all bits to the left by a specified amount of bits. The bit locations that the left shift operator has abandoned are filled with 0. The left shift operator is represented by the symbol.

Example of Left and Right Shift Operators

Output:

Assignment Operators in C

Assignment operators are used to give a variable a value. The assignment operator’s left side operand is a variable, while the assignment operator’s right side operand is a value. Otherwise, the compiler will throw an error if the value on the right side is not of the same data-type as the variable on the left side.

OperatorDescriptionExample
=This is a straightforward assignment operator. Values from the right side operands are assigned to the left side operand.C = A + B will assign the value of A + B to C
+=The AND assignment operator is used to combine two variables. The right operand is added to the left operand, and the result is assigned to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment is a combination of subtraction and assignment. It takes the right operand and subtracts it from the left operand before assigning the result to the left operand.C -= A is equivalent to C = C – A
*=The AND operator multiplies and assigns. It adds the right and left operands together and assigns the result to the left operand.C *= A is equivalent to C = C * A
/=The AND operator divides and assigns. The left operand is divided by the right operand, and the result is assigned to the left operand.C /= A is equivalent to C = C / A
%=The modulus AND assignment operator is a combination of the modulus and the assignment operator. It uses two operands to compute modulus and assigns the result to the left operand.C %= A is equivalent to C = C % A
<<=Operator for both the left shift AND the assignment.C <<= 2 is same as C = C << 2
>>=Operator for both the right shift and the assignment.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

Example of Assignment Operators

When you compile and run the preceding program, you will get the following output:

Output:

Misc. Operators

Aside from the operators mentioned above, the C Language has a few more significant operators, such as sizeof and ?:

OperatorDescriptionExample
sizeof()Returns the variable’s size.sizeof(a), where a is integer, will return 4.
&Returns the variable’s address.&a; returns the actual address of the variable.
*A variable’s pointer.*a;
? :Expression that is conditional.If Condition is true ? then value X : otherwise value Y

Example of Misc. Operators

When you compile and run the preceding program, you will get the following output:

Operators Precedence in C

The order in which terms in an expression are grouped and evaluated is determined by operator precedence. Certain operators take precedence over others; the multiplication operator, for example, takes precedence over the addition operator.

For example, x = 7 + 3 * 2; because operator * has higher precedence than +, x is allocated 13, not 20, and is multiplied by 3*2 before being added to 7.

The highest-priority operators appear at the top of the table, while the lowest-priority operators appear at the bottom. Higher precedence operators will be evaluated first within an expression.

CategoryOperatorAssociativity
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight 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
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

Comma Operator in C

It’s mostly used to separate expressions, variable declarations, and function calls, among other things. It has two operands to operate with. It’s a one-to-one operator. The comma is used as a separator.

Syntax of comma operator:

Summary

A symbol that operates on a variable or value is known as an operator. Operators include arithmetic, logical, conditional, relational, bitwise, and assignment operators, among others. Sizeof(), Pointer operator, Reference operator, and other specific sorts of operators are also available in C.

Related Articles

Inquiries

If you have any questions or suggestions about C Operators: The Complete Guide for Beginners 2021, please feel free to leave a comment below.

Leave a Comment