Thứ Năm, Tháng Ba 6, 2025
spot_img
HomeOperators in C programming

Operators in C programming

Operators in C What is that? The article will tell you operators in C same way to use them.

In the C language, operators are symbols that represent operations performed on one or more operands. They are the basic components of C programming. In this lesson, let's learn about all the built-in operators in C with specific illustrative examples.

What is the C operator?

Operators in C can be defined as symbols that help us perform some specific mathematical, relational, conditional or logical operations on values ​​and variables. The values ​​and variables used with operators are called operands. So we can say that operators are symbols that perform mathematical operations on operands.

For example: c = a + b;

Here, '+' is the operator called addition operator, and 'a' and 'b' are the operands. The addition operator tells the compiler to add both operands 'a' and 'b'.

Although the + operator is often used to add two values ​​together, as in the example above, it can also be used to combine a variable and a value or two variables. For example:

int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)

C divides operators into the following groups:

  • Arithmetic operators
  • Assignment operator
  • Comparison operator
  • Logical operators
  • Bit comparison operator

C There are a series of operators that let you perform different calculations or equations. Here's what you need to know about implementing operators in the C programming language.

The tutorial will explain arithmetic, relational, logical, bit comparison, assignment, and other operators one by one.

Arithmetic operators in C

The following table shows all the arithmetic operators supported by the C language. Assume variable A has the value 10 and variable B has value 20:

Operator Describe For example
+ Add two operands A + B will result in 30
Subtract the value of the second operand from the first operand A – B will result in -10
* Multiply two operands A * B will result in 200
/ Divide the integer part of the two operands B/A will result in 2
% Divide and get the remainder B % A will result in 0
++ The amount of incrementing the operand value by 1 unit A++ will result in 11
The amount of decrement of the operand value by one unit A– will result in 9

Relational operators

The following table shows all the relational operators supported by the C language. Assume that variable A has the value 10 and variable B has value 20, we have:

Operator Describe For example
== Checks if 2 operands are equal or not. If equal then the condition is true. (A == B) is not correct.
!= Checks whether two operands have different values ​​or not. If not equal then the condition is true. (A != B) is true.
> Checks if the left operand has a value greater than the right operand. If greater then the condition is true. (A > B) is not correct.
Checks if the left operand is less than the right operand. If it's smaller, it's true. (A
>= Checks if the left operand has a value greater than or equal to the value of the right operand. If true, then true. (A >= B) is not correct.
Checks if the left operand has a value less than or equal to the right operand. If true, then true. (A

Logical operators

The following table shows all the logical operators supported by the C language. Assume variable A has value 1 and variable B has value 0:

Operator Describe For example
&& Called the logical operator AND (and). If both operators have a non-zero value then the condition becomes true. (A && B) is false.
|| Called the logical operator OR (or). If either operator is non-zero, then the condition is true. (A || B) is true.
! This is called the NOT (negation) operator. Use to reverse the logical state of that operand. If the operand condition is true then its negation will be false. !(A && B) is true.

Bit comparison operator

The bit comparison operator works on bit units, calculating a bitwise comparison expression. The table below for &, |, and ^ is as follows:

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

Suppose if A = 60; and B = 13; then now in binary format 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 bit comparison operators supported by the C language are listed in the table below. Suppose we have variable A with value 60 and variable B with value 13, we have:

Operator Describe For example
& The binary AND (and) operator copies a bit to the result if it exists in both operands. (A & B) will result in 12, i.e. 0000 1100
| The binary OR (or) operator copies a bit to the result if it exists in one or two operands. (A | B) will result in 61, which is 0011 1101
^ The binary XOR operator copies bits that exist in only one operand but not both. (A ^ B) will result in 49, which is 0011 0001
~ Bit inversion operator (inverts bit 1 to bit 0 and vice versa). (~A ) will give the result -61, which is 1100 0011.
Left shift operator. The left operand value is shifted left by the number of bits specified by the right operand. A
>> Right shift operator. The value of the left operand is shifted right by the number of bits specified by the right operand. A >> 2 will result in 15, which is 0000 1111 (shifted right two bits)

Assignment operator

These are the assignment operators supported by the C language:

Operator Describe For example
= Simple assignment operator. Assigns the value of the right operand to the left operand. C = A + B will assign the value of A + B into C
+= Adds the right operand value to the left operand and assigns that value to the left operand. C += A is equivalent to C = C + A
-= Subtract the value of the right operand from the left operand and assign this value to the left operand. C -= A is equivalent to C = C – A
*= Multiply the value of the right operand by the left operand and assign this value to the left operand. C *= A is equivalent to C = C * A
/= Divide the left operand by the right operand and assign this value to the left operand. C /= A is equivalent to C = C / A
%= Take the remainder of dividing the left operand by the right operand and assign it to the left operand. C %= A is equivalent to C = C % A
Shift the left operand to the number of places that is the value of the right operand. C
>>= Right shifts the left operand to the number of places that is the value of the right operand. C >>= 2 is equivalent to C = C >> 2
&= Bitwise AND operation C &= 2 is equivalent to C = C & 2
^= The OR operation eliminates bits C ^= 2 is equivalent to C = C ^ 2
|= Bitwise OR operation. C |= 2 is equivalent to C = C | 2

Mixed operators ↦ sizeof & ternary

There are some important mixed operators namely sizeof and ? : supported by C language.

Operator Describe For example
sizeof() Returns the size of a variable sizeof(a), where a is an integer, returns 4.
& Returns the address of a variable. &a; will give the actual address of variable a.
* Pointer to a variable. *a; will point to variable a.
? : Conditional expression What if the condition is true? then value X : Otherwise value Y

Operator precedence order in C

Operator precedence in C determines how expressions are evaluated. For example, the multiplication operator has precedence over the addition operator, and it is performed first.

For example, x = 7 + 3 * 2; Here, x is assigned the value 13, not 20 because the * operator has higher precedence than the + operator, so it first multiplies 3 * 2 and then adds with 7.

The table below lists the order of precedence of the operators. The operators with the highest precedence appear at the top of the table, and the operators with the lowest precedence appear at the bottom of the table. In an expression, the operators with the highest precedence are evaluated first.

Type Operator Order of priority
Postfix () [] -> . ++ – – Left to right
Unary + – ! ~ ++ – – (type)* & sizeof Right to left
Personality * / % Left to right
Calculate addition + – Left to right
Teleportation > Left to right
Relationship >= Left to right
Balance == != Left to right
Bitwise AND operation & Left to right
Bit XOR operation ^ Left to right
Bitwise OR operation | Left to right
Logical AND operation && Left to right
Logical OR operation || Left to right
Condition ?: Right to left
Assign = += -= *= /= %=>>= Right to left
Comma , Left to right

Previous lesson: Storage class in C programming

Next lesson: Flow control in C programming

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments