/ | ||||
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++20) | ||||
(C++20) |
(C++11) | ||||
expression |
pointer |
specifier | ||||
specifier (C++11) | ||||
specifier (C++11) |
(C++11) | ||||
(C++11) |
(C++11) | ||||
(C++11) |
General | ||||
(C++11) | ||||
(C++20) | ||||
(C++26) | ||||
(C++11) | ||||
(C++11) |
-expression | ||||
-expression | ||||
-expression |
(C++11) | ||||
(C++11) | ||||
(C++17) | ||||
(C++20) |
Assignment operators modify the value of the object.
Operator name | Syntax | Prototype examples (for class T) | ||
---|---|---|---|---|
Inside class definition | Outside class definition | |||
simple assignment | Yes | T& T::operator =(const T2& b); | ||
addition assignment | Yes | T& T::operator +=(const T2& b); | T& operator +=(T& a, const T2& b); | |
subtraction assignment | Yes | T& T::operator -=(const T2& b); | T& operator -=(T& a, const T2& b); | |
multiplication assignment | Yes | T& T::operator *=(const T2& b); | T& operator *=(T& a, const T2& b); | |
division assignment | Yes | T& T::operator /=(const T2& b); | T& operator /=(T& a, const T2& b); | |
remainder assignment | Yes | T& T::operator %=(const T2& b); | T& operator %=(T& a, const T2& b); | |
bitwise AND assignment | Yes | T& T::operator &=(const T2& b); | T& operator &=(T& a, const T2& b); | |
bitwise OR assignment | Yes | T& T::operator |=(const T2& b); | T& operator |=(T& a, const T2& b); | |
bitwise XOR assignment | Yes | T& T::operator ^=(const T2& b); | T& operator ^=(T& a, const T2& b); | |
bitwise left shift assignment | Yes | T& T::operator <<=(const T2& b); | T& operator <<=(T& a, const T2& b); | |
bitwise right shift assignment | Yes | T& T::operator >>=(const T2& b); | T& operator >>=(T& a, const T2& b); | |
this, and most also return *this so that the user-defined operators can be used in the same manner as the built-ins. However, in a user-defined operator overload, any type can be used as return type (including void). can be any type including . |
Definitions Assignment operator syntax Built-in simple assignment operator Assignment from an expression Assignment from a non-expression initializer clause Built-in compound assignment operator Example Defect reports See also |
Copy assignment replaces the contents of the object a with a copy of the contents of b ( b is not modified). For class types, this is performed in a special member function, described in copy assignment operator .
replaces the contents of the object a with the contents of b, avoiding copying if possible (b may be modified). For class types, this is performed in a special member function, described in . | (since C++11) |
For non-class types, copy and move assignment are indistinguishable and are referred to as direct assignment .
Compound assignment replace the contents of the object a with the result of a binary operation between the previous value of a and the value of b .
The assignment expressions have the form
target-expr new-value | (1) | ||||||||
target-expr op new-value | (2) | ||||||||
target-expr | - | the expression to be assigned to |
op | - | one of *=, /= %=, += -=, <<=, >>=, &=, ^=, |= |
new-value | - | the expression (until C++11) (since C++11) to assign to the target |
If new-value is not an expression, the assignment expression will never match an overloaded compound assignment operator. | (since C++11) |
For the built-in simple assignment, the object referred to by target-expr is modified by replacing its value with the result of new-value . target-expr must be a modifiable lvalue.
The result of a built-in simple assignment is an lvalue of the type of target-expr , referring to target-expr . If target-expr is a bit-field , the result is also a bit-field.
If new-value is an expression, it is implicitly converted to the cv-unqualified type of target-expr . When target-expr is a bit-field that cannot represent the value of the expression, the resulting value of the bit-field is implementation-defined.
If target-expr and new-value identify overlapping objects, the behavior is undefined (unless the overlap is exact and the type is the same).
If the type of target-expr is volatile-qualified, the assignment is deprecated, unless the (possibly parenthesized) assignment expression is a or an . | (since C++20) |
new-value is only allowed not to be an expression in following situations: is of a , and new-value is empty or has only one element. In this case, given an invented variable t declared and initialized as T t = new-value , the meaning of x = new-value is x = t. is of class type. In this case, new-value is passed as the argument to the assignment operator function selected by . <double> z; z = {1, 2}; // meaning z.operator=({1, 2}) z += {1, 2}; // meaning z.operator+=({1, 2}) int a, b; a = b = {1}; // meaning a = b = 1; a = {1} = b; // syntax error | (since C++11) |
In overload resolution against user-defined operators , for every type T , the following function signatures participate in overload resolution:
& operator=(T*&, T*); | ||
volatile & operator=(T*volatile &, T*); | ||
For every enumeration or pointer to member type T , optionally volatile-qualified, the following function signature participates in overload resolution:
operator=(T&, T); | ||
For every pair A1 and A2 , where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signature participates in overload resolution:
operator=(A1&, A2); | ||
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.
The requirements on target-expr and new-value of built-in simple assignment operators also apply. Furthermore:
In overload resolution against user-defined operators , for every pair A1 and A2 , where A1 is an arithmetic type (optionally volatile-qualified) and A2 is a promoted arithmetic type, the following function signatures participate in overload resolution:
operator*=(A1&, A2); | ||
operator/=(A1&, A2); | ||
operator+=(A1&, A2); | ||
operator-=(A1&, A2); | ||
For every pair I1 and I2 , where I1 is an integral type (optionally volatile-qualified) and I2 is a promoted integral type, the following function signatures participate in overload resolution:
operator%=(I1&, I2); | ||
operator<<=(I1&, I2); | ||
operator>>=(I1&, I2); | ||
operator&=(I1&, I2); | ||
operator^=(I1&, I2); | ||
operator|=(I1&, I2); | ||
For every optionally cv-qualified object type T , the following function signatures participate in overload resolution:
& operator+=(T*&, ); | ||
& operator-=(T*&, ); | ||
volatile & operator+=(T*volatile &, ); | ||
volatile & operator-=(T*volatile &, ); | ||
Possible output:
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
C++11 | for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a user-defined assignment operator | removed user-defined assignment constraint | |
C++11 | E1 = {E2} was equivalent to E1 = T(E2) ( is the type of ), this introduced a C-style cast | it is equivalent to E1 = T{E2} | |
C++20 | compound assignment operators for volatile -qualified types were inconsistently deprecated | none of them is deprecated | |
C++11 | an assignment from a non-expression initializer clause to a scalar value would perform direct-list-initialization | performs copy-list- initialization instead | |
C++20 | bitwise compound assignment operators for volatile types were deprecated while being useful for some platforms | they are not deprecated |
Operator precedence
Operator overloading
Common operators | ||||||
---|---|---|---|---|---|---|
a = b | ++a | +a | !a | a == b | a[...] | function call |
a(...) | ||||||
comma | ||||||
a, b | ||||||
conditional | ||||||
a ? b : c | ||||||
Special operators | ||||||
converts one type to another related type |
for Assignment operators |
There are 6 basic ways to initialize variables in C++:
Much like copy assignment, this copies the value on the right-hand side of the equals into the variable being created on the left-hand side. In the above snippet, variable width will be initialized with value 5 .
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The assignment operator = assigns the value of its right-hand operand to a variable, a property , or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand. The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it.
The assignment operator = is right-associative, that is, an expression of the form
is evaluated as
The following example demonstrates the usage of the assignment operator with a local variable, a property, and an indexer element as its left-hand operand:
The left-hand operand of an assignment receives the value of the right-hand operand. When the operands are of value types , assignment copies the contents of the right-hand operand. When the operands are of reference types , assignment copies the reference to the object.
This is called value assignment : the value is assigned.
Ref assignment = ref makes its left-hand operand an alias to the right-hand operand, as the following example demonstrates:
In the preceding example, the local reference variable arrayElement is initialized as an alias to the first array element. Then, it's ref reassigned to refer to the last array element. As it's an alias, when you update its value with an ordinary assignment operator = , the corresponding array element is also updated.
The left-hand operand of ref assignment can be a local reference variable , a ref field , and a ref , out , or in method parameter. Both operands must be of the same type.
For a binary operator op , a compound assignment expression of the form
is equivalent to
except that x is only evaluated once.
Compound assignment is supported by arithmetic , Boolean logical , and bitwise logical and shift operators.
You can use the null-coalescing assignment operator ??= to assign the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null . For more information, see the ?? and ??= operators article.
A user-defined type can't overload the assignment operator. However, a user-defined type can define an implicit conversion to another type. That way, the value of a user-defined type can be assigned to a variable, a property, or an indexer element of another type. For more information, see User-defined conversion operators .
A user-defined type can't explicitly overload a compound assignment operator. However, if a user-defined type overloads a binary operator op , the op= operator, if it exists, is also implicitly overloaded.
For more information, see the Assignment operators section of the C# language specification .
Operators in programming are essential symbols that perform operations on variables and values , enabling tasks like arithmetic calculations, logical comparisons, and bitwise manipulations. In this article, we will learn about the basics of operators and their types.
Operators in Programming
Table of Content
Operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables , constants , or values , and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.
Operator Symbol | Name | Type | Description | Use |
---|---|---|---|---|
+ | Addition | Arithmetic Operators | Adds two values | result = num1 + num2; |
– | Subtraction | Subtracts the right operand from the left | result = num1 – num2; | |
* | Multiplication | Multiplies two values | result = num1 * num2; | |
/ | Division | Divides the left operand by the right | result = num1 / num2; | |
% | Modulus | Returns the remainder of division | result = num1 % num2; | |
++ | Increment | Unary Operators | Increases the value of a variable by 1 | num++; or ++num; |
— | Decrement | Decreases the value of a variable by 1 | num–; or –num; | |
= | Assignment | Assignment Operators | Assigns a value to a variable | x = 10; |
+= | Add and Assign | Adds the right operand to the left and assigns | x += 5; (equivalent to x = x + 5;) | |
-= | Subtract and Assign | Subtracts the right operand and assigns | x -= 3; (equivalent to x = x – 3;) | |
*= | Multiply and Assign | Multiplies the right operand and assigns | x *= 2; (equivalent to x = x * 2;) | |
/= | Divide and Assign | Divides the left operand and assigns | x /= 4; (equivalent to x = x / 4;) | |
%= | Modulus and Assign | Computes modulus and assigns | x %= 3; (equivalent to x = x % 3;) | |
== | Equal to | Relational or Comparison Operators | Tests if two values are equal | if (a == b) |
!= | Not Equal to | Tests if two values are not equal | if (a != b) | |
< | Less Than | Tests if the left value is less than the right | if (a < b) | |
> | Greater Than | Tests if the left value is greater than right | if (a > b) | |
<= | Less Than or Equal To | Tests if the left value is less than or equal | if (a <= b) | |
>= | Greater Than or Equal To | Tests if the left value is greater than or equal | if (a >= b) | |
&& | Logical AND | Logical Operators | Returns true if both operands are true | if (a && b) |
|| | Logical OR | |||
! | Logical NOT | Reverses the logical state of its operand | if (!condition) | |
& | Bitwise AND | Bitwise Operators | Performs bitwise AND on individual bits | result = a & b; |
` | Bitwise OR | Performs bitwise OR on individual bits | ||
^ | Bitwise XOR | Performs bitwise XOR on individual bits | result = a ^ b; | |
~ | Bitwise NOT | Inverts the bits of its operand | result = ~a; | |
<< | Left Shift | Shifts bits to the left | result = a << 2; | |
>> | Right Shift | Shifts bits to the right | result = a >> 1; | |
?: | Conditional (Ternary) | Conditional Operator | Evaluates a condition and returns one of two values | result = (condition) ? value1 : value2; |
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.
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
Can anybody give a clear explanation of how variable assignment really works in Makefiles.
What is the difference between :
I have read the section in GNU Make's manual, but it still doesn't make sense to me.
Normal setting of a variable, but any other variables mentioned with the value field are recursively expanded with their value at the point at which the variable is used, not the one it had when it was declared
Setting of a variable with simple expansion of the values inside - values within it are expanded at declaration time.
The value you specify is installed verbatim; if it contains references to other variables, these references are expanded whenever this variable is substituted (in the course of expanding some other string). When this happens, it is called recursive expansion .
The catch : foo will be expanded to the value of $(bar) each time foo is evaluated, possibly resulting in different values. Surely you cannot call it "lazy"! This can surprise you if executed on midnight:
Variables defined with ‘:=’ or ‘::=’ are simply expanded variables.
Simply expanded variables are defined by lines using ‘:=’ or ‘::=’ [...]. Both forms are equivalent in GNU make; however only the ‘::=’ form is described by the POSIX standard [...] 2012.
The value of a simply expanded variable is scanned once and for all, expanding any references to other variables and functions, when the variable is defined.
Not much to add. It's evaluated immediately, including recursive expansion of, well, recursively expanded variables.
The catch : If VARIABLE refers to ANOTHER_VARIABLE :
and ANOTHER_VARIABLE is not defined before this assignment, ANOTHER_VARIABLE will expand to an empty value.
is equivalent to
where $(origin FOO) equals to undefined only if the variable was not set at all.
The catch : if FOO was set to an empty string, either in makefiles, shell environment, or command line overrides, it will not be assigned bar .
Appending :
When the variable in question has not been defined before, ‘+=’ acts just like normal ‘=’: it defines a recursively-expanded variable. However, when there is a previous definition, exactly what ‘+=’ does depends on what flavor of variable you defined originally.
So, this will print foo bar :
but this will print foo :
The catch is that += behaves differently depending on what type of variable VAR was assigned before.
The syntax to assign multiline value to a variable is:
Assignment operator can be omitted, then it creates a recursively-expanded variable.
The last newline before endef is removed.
is the same as
Don't use it. $(shell) call is more readable, and the usage of both in a makefiles is highly discouraged. At least, $(shell) follows Joel's advice and makes wrong code look obviously wrong .
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
IMAGES
COMMENTS
Python's assignment operators allow you to define assignment statements. This type of statement lets you create, initialize, and update variables throughout your code. Variables are a fundamental cornerstone in every piece of code, and assignment statements give you complete control over variable creation and mutation.
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. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...
An assignment operator is an operator that is used to assign some value to a variable. Like normally in Python, we write "a = 5" to assign value 5 to variable 'a'. Augmented assignment operators have a special role to play in Python programming. It basically combines the functioning of the arithmetic or bitwise operator with the assignment operator
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. This operator first adds the current value of the variable on left to the value on the right and ...
Variables and Assignment¶. When programming, it is useful to be able to store information in variables. A variable is a string of characters and numbers associated with a piece of information. The assignment operator, denoted by the "=" symbol, is the operator that is used to assign values to variables in Python.The line x=1 takes the known value, 1, and assigns that value to the variable ...
Assignment (computer science) In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location (s) denoted by a variable name; in other words, it copies a value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct.
Assignment Operator. The assignment operator allows us to change the value of a modifiable data object (for beginning programmers this typically means a variable). It is associated with the concept of moving a value into the storage location (again usually a variable). Within C++ programming language the symbol used is the equal symbol.
Python Assignment Operators. Assignment operators are used to assign values to variables: Operator. Example. Same As. Try it. =. x = 5. x = 5.
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: Example. int x = 10; Try it Yourself » The addition assignment operator (+=) adds a value to a variable:
In Python, variables need not be declared or defined in advance, as is the case in many other programming languages. To create a variable, you just assign it a value and then start using it. Assignment is done with a single equals sign ( = ): Python. >>> n = 300. This is read or interpreted as " n is assigned the value 300 .".
Assignment (=) The assignment ( =) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables.
The language definition simply states: An assignment operator stores a value in the object designated by the left operand. (6.5.16, para 3). The only general constraint is that the left operand be a modifiable lvalue. An lvalue can correspond to a register (which has no address) or an addressable memory location.
Note: The compound assignment operator in Java performs implicit type casting. Let's consider a scenario where x is an int variable with a value of 5. int x = 5; If you want to add the double value 4.5 to the integer variable x and print its value, there are two methods to achieve this: Method 1: x = x + 4.5. Method 2: x += 4.5.
for assignments to class type objects, the right operand could be an initializer list only when the assignment is defined by a user-defined assignment operator. removed user-defined assignment constraint. CWG 1538. C++11. E1 ={E2} was equivalent to E1 = T(E2) ( T is the type of E1 ), this introduced a C-style cast. it is equivalent to E1 = T{E2}
int x; // define an integer variable named x int y, z; // define two integer variables, named y and z. Variable assignment. After a variable has been defined, you can give it a value (in a separate statement) using the = operator. This process is called assignment, and the = operator is called the assignment operator.
The assignment operator = assigns the value of its right-hand operand to a variable, a property, or an indexer element given by its left-hand operand. The result of an assignment expression is the value assigned to the left-hand operand. The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly ...
Assignment Operator: An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands. Unlike in C++, assignment ...
In C++, the addition assignment operator (+=) combines the addition operation with the variable assignment allowing you to increment the value of variable by a specified expression in a concise and efficient way. Syntax. variable += value; This above expression is equivalent to the expression: variable = variable + value; Example.
ClassName = Other.ClassName; return *this; } This is the general convention used when overloading operator=. The return statement allows chaining of assignments (like a = b = c) and passing the parameter by const reference avoids copying Other on its way into the function call. edited Dec 22, 2010 at 13:54.
Operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables, constants, or values, and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise ...
The syntax to assign multiline value to a variable is: define VAR_NAME := line line endef or. define VAR_NAME = line line endef Assignment operator can be omitted, then it creates a recursively-expanded variable. define VAR_NAME line line endef The last newline before endef is removed. Bonus: the shell assignment operator '!=' HASH ...