Here are some common types of operators:
These operators provide the building blocks for creating complex expressions and performing diverse operations in programming languages. Understanding their usage is crucial for writing efficient and expressive code.
Arithmetic operators in programming are fundamental components of programming languages, enabling the manipulation of numeric values for various computational tasks. Here’s an elaboration on the key arithmetic operators:
Operator | Description | Examples |
---|---|---|
+ (Addition) | Combines two numeric values, yielding their sum. | (result will be 8) |
– (Subtraction) | Subtracts the right operand from the left operand. | (difference will be 6) |
* (Multiplication) | Multiplies two numeric values, producing their product. | (product will be 21) |
/ (Division) | Divides the left operand by the right operand, producing a quotient. | (quotient will be 5) |
% (Modulo) | Returns the remainder after the division of the left operand by the right operand. | (remainder will be 1) |
Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:
Operator | Description | Examples |
---|---|---|
== (Equal to) | Checks if the values on both sides are equal. | (evaluates to true) |
!= (Not equal to) | Checks if the values on both sides are not equal. | (evaluates to true) |
< (Less than) | Tests if the value on the left is less than the value on the right. | (evaluates to true) |
> (Greater than) | Tests if the value on the left is greater than the value on the right. | (evaluates to true) |
<= (Less than or equal to) | Checks if the value on the left is less than or equal to the value on the right. | (evaluates to true) |
>= (Greater than or equal to) | Checks if the value on the left is greater than or equal to the value on the right. | (evaluates to true) |
These operators are extensively used in conditional statements, loops, and decision-making constructs to control the flow of a program based on the relationship between variables or values. Understanding comparison operators is crucial for creating logical and effective algorithms in programming.
Logical operators in programming are used to perform logical operations on Boolean values . These operators are crucial for combining or manipulating conditions and controlling the flow of a program based on logical expressions. Here are the common logical operators:
Operator | Description | Examples |
---|---|---|
&& (Logical AND) | Returns true if both operands are true; otherwise, it returns false. | (evaluates to false) |
(||) Logical OR | Returns true if at least one of the operands is true; otherwise, it returns false | true || false; (evaluates to true) |
! (Logical NOT) | Returns true if the operand is false and vice versa; it negates the Boolean value. | (evaluates to false) |
These logical operators are frequently used in conditional statements (if, else if, else), loops, and decision-making constructs to create complex conditions based on multiple Boolean expressions. Understanding how to use logical operators is essential for designing effective and readable control flow in programming.
Assignment operators in programming are used to assign values to variables. They are essential for storing and updating data within a program. Here are common assignment operators:
Operator | Description | Examples |
---|---|---|
= (Assignment) | Assigns the value on the right to the variable on the left. | assigns the value 10 to the variable x. |
+= (Addition Assignment) | Adds the value on the right to the current value of the variable on the left and assigns the result to the variable. | is equivalent to |
-= (Subtraction Assignment) | Subtracts the value on the right from the current value of the variable on the left and assigns the result to the variable. | is equivalent to |
*= (Multiplication Assignment) | Multiplies the current value of the variable on the left by the value on the right and assigns the result to the variable. | is equivalent to |
/= (Division Assignment) | Divides the current value of the variable on the left by the value on the right and assigns the result to the variable. | is equivalent to |
%= (Modulo Assignment) | Calculates the modulo of the current value of the variable on the left and the value on the right, then assigns the result to the variable. | is equivalent to |
Assignment operators are fundamental for updating variable values, especially in loops and mathematical computations, contributing to the dynamic nature of programming. Understanding how to use assignment operators is essential for effective variable manipulation in a program.
Increment and decrement operators in programming are used to increase or decrease the value of a variable by 1, respectively. They are shorthand notations for common operations and are particularly useful in loops. Here are the two types:
Operator | Description | Examples |
---|---|---|
++ (Increment) | Increases the value of a variable by 1. | is equivalent to or |
— (Decrement) | Decreases the value of a variable by 1. | is equivalent to or |
These operators are frequently employed in loops, especially for iterating through arrays or performing repetitive tasks. Their concise syntax enhances code readability and expressiveness.
Bitwise operators in programming perform operations at the bit level , manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:
Operator | Description | Examples |
---|---|---|
& (Bitwise AND) | Performs a bitwise AND operation between corresponding bits of two operands. | sets each bit to 1 if both corresponding bits in A and B are 1. |
| (Bitwise OR) | Performs a bitwise OR operation between corresponding bits of two operands. | A | B sets each bit to 1 if at least one corresponding bit in A or B is 1. |
^ (Bitwise XOR) | Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two operands. | sets each bit to 1 if the corresponding bits in A and B are different. |
~ (Bitwise NOT) | Inverts the bits of a single operand, turning 0s to 1s and vice versa. | inverts all bits of A. |
<< (Left Shift) | Shifts the bits of the left operand to the left by a specified number of positions. | shifts the bits of A two positions to the left. |
>> (Right Shift) | Shifts the bits of the left operand to the right by a specified number of positions. | shifts the bits of A three positions to the right. |
Bitwise operators are useful in scenarios where direct manipulation of binary representations or specific bit patterns is required, such as optimizing certain algorithms or working with hardware interfaces. Understanding bitwise operations is essential for low-level programming tasks.
Operator Precedence is a rule that determines the order in which operators are evaluated in an expression. It defines which operators take precedence over others when they are combined in the same expression. Operators with higher precedence are evaluated before operators with lower precedence. Parentheses can be used to override the default precedence and explicitly specify the order of evaluation.
Operator Associativity is a rule that determines the grouping of operators with the same precedence in an expression when they appear consecutively. It specifies the direction in which operators of equal precedence are evaluated. The two common associativities are:
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | () | Parentheses | Left-to-Right |
x++, x– | Postfix increment, decrement | ||
2 | ++x, –x | Prefix increment, decrement | Right-to-Left |
‘+’ , ‘-‘ | Unary plus, minus | ||
! , ~ | Logical NOT, Bitwise complement | ||
* | Dereference Operator | ||
& | Addressof Operator | ||
3 | *, /, % | Multiplication, division, modulus | Left-to-Right |
4 | +, – | Addition, subtraction | Left-to-Right |
5 | << , >> | Bitwise shift left, Bitwise shift right | Left-to-Right |
6 | < , <= | Relational less than, less than or equal to | Left-to-Right |
> , >= | Relational greater than, greater than or equal to | ||
7 | == , != | Relational is equal to, is not equal to | Left-to-Right |
8 | & | Bitwise AND | Left-to-Right |
9 | ^ | Bitwise XOR | Left-to-Right |
10 | | | Bitwise OR | Left-to-Right |
11 | && | Logical AND | Left-to-Right |
12 | || | Logical OR | Left-to-Right |
13 | ?: | Ternary conditional | Right-to-Left |
14 | = | Assignment | Right-to-Left |
+= , -= | Addition, subtraction assignment | ||
*= , /= | Multiplication, division assignment | ||
%= , &= | Modulus, bitwise AND assignment | ||
^= , |= | Bitwise exclusive, inclusive OR assignment | ||
<<=, >>= | Bitwise shift left, right assignment | ||
15 | , | comma (expression separator) | Left-to-Right |
Here are some frequently asked questions (FAQs) related to programming operators:
A: Operators in programming are symbols that represent computations or actions to be performed on operands. They can manipulate data, perform calculations, and facilitate various operations in a program.
A: Operators are categorized based on their functionality. Common categories include arithmetic operators (for mathematical operations), assignment operators (for assigning values), comparison operators (for comparing values), logical operators (for logical operations), and bitwise operators (for manipulating individual bits).
A: Unary operators operate on a single operand, while binary operators operate on two operands. For example, the unary minus -x negates the value of x , while the binary plus a + b adds the values of a and b .
A: Yes, some programming languages support operator overloading, allowing developers to define custom behaviors for operators when applied to user-defined types. This is commonly seen in languages like C++.
A: The logical AND ( && ) operator returns true if both of its operands are true. The logical OR ( || ) operator returns true if at least one of its operands is true. These operators are often used in conditional statements and expressions.
A: The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false. It is often used for concise conditional assignments.
A: The bitwise XOR ( ^ ) operator performs an exclusive OR operation on individual bits. It returns 1 for bits that are different and 0 for bits that are the same. This operator is commonly used in bit manipulation tasks.
A: The = operator is an assignment operator used to assign a value to a variable. The == operator is a comparison operator used to check if two values are equal. It is important not to confuse the two, as = is used for assignment, and == is used for comparison.
A: The increment ( ++ ) operator adds 1 to the value of a variable, while the decrement ( -- ) operator subtracts 1. These operators can be used as prefix ( ++x ) or postfix ( x++ ), affecting the order of the increment or decrement operation.
A: While many operators are common across programming languages, some languages may introduce unique operators or have variations in their behavior. However, the fundamental concepts of arithmetic, logical, and bitwise operations are prevalent across various languages.
A: The modulus operator ( % ) returns the remainder when one number is divided by another. It is often used to check divisibility or to cycle through a sequence of values. For example, a % 2 can be used to determine if a is an even or odd number.
A: Yes, the same symbol may represent different operators or operations in different programming languages. For example, the + operator is used for addition in most languages, but in some languages (like JavaScript), it is also used for string concatenation.
A: Short-circuit evaluation is a behavior where the second operand of a logical AND ( && ) or logical OR ( || ) operator is not evaluated if the outcome can be determined by the value of the first operand alone. This can lead to more efficient code execution.
A: The bitwise left shift ( << ) operator shifts the bits of a binary number to the left, effectively multiplying the number by 2. The bitwise right shift ( >> ) operator shifts the bits to the right, effectively dividing the number by 2.
A: Operator precedence determines the order in which operators are evaluated. For example, in the expression a + b * c , the multiplication ( * ) has higher precedence than addition ( + ), so b * c is evaluated first.
A: The ternary operator ( ?: ) is a concise way to express a conditional statement with a single line of code. It returns one of two values based on a condition. An if-else statement provides a more extensive code block for handling multiple statements based on a condition.
A: Some programming languages provide specific operators or methods for working with arrays or collections. For example, in Python, the in operator is used to check if an element is present in a list.
A: Bitwise operators are often used for efficient memory management by manipulating individual bits. For example, bitwise AND can be used to mask specific bits, and bitwise OR can be used to set particular bits.
A: No, not all programming languages support operator overloading. While some languages like C++ allow developers to redefine the behavior of operators for user-defined types, others, like Java, do not permit operator overloading.
A: Parentheses are used to explicitly specify the order of operations in complex expressions, ensuring that certain operations are performed before others. For example, (a + b) * c ensures that addition is performed before multiplication.
In conclusion, operators in programming are essential tools that enable the manipulation, comparison, and logical operations on variables and values. They form the building blocks of expressions and play a fundamental role in controlling program flow, making decisions, and performing calculations. From arithmetic and comparison operators for numerical tasks to logical operators for decision-making, and bitwise operators for low-level bit manipulation, each type serves specific purposes in diverse programming scenarios.
Similar reads.
Assignment operators.
(C11) | ||||
Miscellaneous | ||||
General | ||||
(C11) | ||||
(C99) | ||||
Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right.
Operator | Operator name | Example | Description | Equivalent of |
---|---|---|---|---|
= | basic assignment | a = b | becomes equal to | |
+= | addition assignment | a += b | becomes equal to the addition of and | a = a + b |
-= | subtraction assignment | a -= b | becomes equal to the subtraction of from | a = a - b |
*= | multiplication assignment | a *= b | becomes equal to the product of and | a = a * b |
/= | division assignment | a /= b | becomes equal to the division of by | a = a / b |
%= | modulo assignment | a %= b | becomes equal to the remainder of divided by | a = a % b |
&= | bitwise AND assignment | a &= b | becomes equal to the bitwise AND of and | a = a & b |
|= | bitwise OR assignment | a |= b | becomes equal to the bitwise OR of and | a = a | b |
^= | bitwise XOR assignment | a ^= b | becomes equal to the bitwise XOR of and | a = a ^ b |
<<= | bitwise left shift assignment | a <<= b | becomes equal to left shifted by | a = a << b |
>>= | bitwise right shift assignment | a >>= b | becomes equal to right shifted by | a = a >> b |
Simple assignment Notes Compound assignment References See Also See also |
The simple assignment operator expressions have the form
lhs rhs | |||||||||
lhs | - | expression of any complete object type |
rhs | - | expression of any type to lhs or with lhs |
Assignment performs implicit conversion from the value of rhs to the type of lhs and then replaces the value in the object designated by lhs with the converted value of rhs .
Assignment also returns the same value as what was stored in lhs (so that expressions such as a = b = c are possible). The value category of the assignment operator is non-lvalue (so that expressions such as ( a = b ) = c are invalid).
rhs and lhs must satisfy one of the following:
has type (possibly qualified or atomic(since C11)) _Bool and rhs is a pointer or a value(since C23) | (since C99) |
has type (possibly qualified or atomic) and rhs has type | (since C23) |
If rhs and lhs overlap in memory (e.g. they are members of the same union), the behavior is undefined unless the overlap is exact and the types are compatible .
Although arrays are not assignable, an array wrapped in a struct is assignable to another object of the same (or compatible) struct type.
The side effect of updating lhs is sequenced after the value computations, but not the side effects of lhs and rhs themselves and the evaluations of the operands are, as usual, unsequenced relative to each other (so the expressions such as i = ++ i ; are undefined)
Assignment strips extra range and precision from floating-point expressions (see FLT_EVAL_METHOD ).
In C++, assignment operators are lvalue expressions, not so in C.
The compound assignment operator expressions have the form
lhs op rhs | |||||||||
op | - | one of *=, /= %=, += -=, <<=, >>=, &=, ^=, |= |
lhs, rhs | - | expressions with (where lhs may be qualified or atomic), except when op is += or -=, which also accept pointer types with the same restrictions as + and - |
The expression lhs @= rhs is exactly the same as lhs = lhs @ ( rhs ) , except that lhs is evaluated only once.
If lhs has type, the operation behaves as a single atomic read-modify-write operation with memory order . For integer atomic types, the compound assignment @= is equivalent to: addr = &lhs; T2 val = rhs; T1 old = *addr; T1 new; do { new = old @ val } while (! (addr, &old, new); | (since C11) |
Operator precedence
Common operators | ||||||
---|---|---|---|---|---|---|
a = b | ++a | +a | !a | a == b | a[b] | a(...) |
for Assignment operators |
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
An assignment operation assigns the value of the right-hand operand to the storage location named by the left-hand operand. Therefore, the left-hand operand of an assignment operation must be a modifiable l-value. After the assignment, an assignment expression has the value of the left operand but isn't an l-value.
assignment-expression : conditional-expression unary-expression assignment-operator assignment-expression
assignment-operator : one of = *= /= %= += -= <<= >>= &= ^= |=
The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators:
Operator | Operation Performed |
---|---|
Simple assignment | |
Multiplication assignment | |
Division assignment | |
Remainder assignment | |
Addition assignment | |
Subtraction assignment | |
Left-shift assignment | |
Right-shift assignment | |
Bitwise-AND assignment | |
Bitwise-exclusive-OR assignment | |
Bitwise-inclusive-OR assignment |
In assignment, the type of the right-hand value is converted to the type of the left-hand value, and the value is stored in the left operand after the assignment has taken place. The left operand must not be an array, a function, or a constant. The specific conversion path, which depends on the two types, is outlined in detail in Type Conversions .
Was this page helpful?
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .
Submit and view feedback for
C++ functions, c++ classes, c++ reference, c++ examples, c++ assignment operators, assignment operators.
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator ( = ) to assign the value 10 to a variable called x :
The addition assignment operator ( += ) adds a value to a variable:
A list of all assignment operators:
Operator | Example | Same As | Try it |
---|---|---|---|
= | x = 5 | x = 5 | |
+= | x += 3 | x = x + 3 | |
-= | x -= 3 | x = x - 3 | |
*= | x *= 3 | x = x * 3 | |
/= | x /= 3 | x = x / 3 | |
%= | x %= 3 | x = x % 3 | |
&= | x &= 3 | x = x & 3 | |
|= | x |= 3 | x = x | 3 | |
^= | x ^= 3 | x = x ^ 3 | |
>>= | x >>= 3 | x = x >> 3 | |
<<= | x <<= 3 | x = x << 3 |
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]
Top references, top examples, get certified.
In C language, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable, or an expression.
The value to be assigned forms the right-hand operand, whereas the variable to be assigned should be the operand to the left of the " = " symbol, which is defined as a simple assignment operator in C.
In addition, C has several augmented assignment operators.
The following table lists the assignment operators supported by the C language −
Operator | Description | Example |
---|---|---|
= | Simple assignment operator. Assigns values from right side operands to left side operand | C = A + B will assign the value of A + B to C |
+= | Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator. | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator. | 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 |
The = operator is one of the most frequently used operators in C. As per the ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed.
You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.
You can use a literal, another variable, or an expression in the assignment statement.
Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.
In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.
On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −
In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.
For example, the expression "a += b" has the same effect of performing "a + b" first and then assigning the result back to the variable "a".
Run the code and check its output −
Similarly, the expression "a <<= b" has the same effect of performing "a << b" first and then assigning the result back to the variable "a".
Here is a C program that demonstrates the use of assignment operators in C −
When you compile and execute the above program, it will produce the following result −
IMAGES
VIDEO
COMMENTS
What are Assignment Operators? Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data.
Different types of assignment operators are shown below: 1. “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. Example: a = 10; b = 20; ch = 'y'; 2. “+=”: This operator is combination of ‘+’ and ‘=’ operators.
Assignment Operators: Assign values to variables. Examples: = (assign), += (add and assign), -=, *= (multiply and assign), /=, %=. Increment and Decrement Operators: Increase or decrease the value of a variable by 1. Examples: ++ (increment), — (decrement).
There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand. compound assignment, in which an arithmetic, shift, or bitwise operation is performed before storing the result.
Built-in compound assignment operator. The behavior of every built-in compound-assignment expression target-expr op = new-value is exactly the same as the behavior of the expression target-expr = target-expr op new-value, except that target-expr is evaluated only once.
Assignment and compound assignment operators are binary operators that modify the variable to their left using the value to their right.
= *= /= %= += -= <<= >>= &= ^= |= The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators: Expand table.
Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( =) to assign the value 10 to a variable called x: Example. int x = 10; Try it Yourself » The addition assignment operator ( +=) adds a value to a variable: Example. int x = 10; x += 5; Try it Yourself »
Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( =) to assign the value 10 to a variable called x:
In C language, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable, or an expression.