Transact-SQL
Reinforcement Learning
R Programming
React Native
Python Design Patterns
Python Pillow
Python Turtle
Verbal Ability
Interview Questions
Company Questions
Artificial Intelligence
Cloud Computing
Data Science
Machine Learning
Data Structures
Operating System
Computer Network
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
C Programming
Control System
Data Mining
Data Warehouse
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.
Why does an expression i = 2 return 2? What is the rule this is based on?
printf("%d\n", i = 2 ); /* prints 2 */
I am in C domain after spending long time in Java/C#. Forgive my ignorance.
It evaluates to 2 because that's how the standard defines it. From C11 Standard , section 6.5.16:
An assignment expression has the value of the left operand after the assignment
It's to allow things like this:
(although there's some debate as to whether code like that is a good thing or not.)
Incidentally, this behaviour is replicated in Java (and I would bet that it's the same in C# too).
The rule is to return the right-hand operand of = converted to the type of the variable which is assigned to.
It consider the expression firstly then print the leftmost variable.
x++ is postfix and ++x is prefix so:
In C (almost) all expressions have 2 things 1) a value 2) a side effect
The value of the expression
is 2 ; its side effect is "none";
is 2 ; its side effect is "changing the value in the object named i to 2";
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 .
C operator precedence.
(C11) | ||||
Miscellaneous | ||||
General | ||||
(C11) | ||||
(C99) | ||||
The following table lists the precedence and associativity of C operators. Operators are listed top to bottom, in descending precedence.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | Suffix/postfix increment and decrement | Left-to-right | |
Function call | |||
Array subscripting | |||
Structure and union member access | |||
Structure and union member access through pointer | |||
){ } | Compound literal(C99) | ||
2 | Prefix increment and decrement | Right-to-left | |
Unary plus and minus | |||
Logical NOT and bitwise NOT | |||
) | Cast | ||
Indirection (dereference) | |||
Address-of | |||
Size-of | |||
Alignment requirement(C11) | |||
3 | Multiplication, division, and remainder | Left-to-right | |
4 | Addition and subtraction | ||
5 | Bitwise left shift and right shift | ||
6 | For relational operators < and ≤ respectively | ||
For relational operators > and ≥ respectively | |||
7 | For relational = and ≠ respectively | ||
8 | Bitwise AND | ||
9 | Bitwise XOR (exclusive or) | ||
10 | Bitwise OR (inclusive or) | ||
11 | Logical AND | ||
12 | Logical OR | ||
13 | Ternary conditional | Right-to-left | |
14 | Simple assignment | ||
Assignment by sum and difference | |||
Assignment by product, quotient, and remainder | |||
Assignment by bitwise left shift and right shift | |||
Assignment by bitwise AND, XOR, and OR | |||
15 | Comma | Left-to-right |
When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expression * p ++ is parsed as * ( p ++ ) , and not as ( * p ) ++ .
Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a = b = c is parsed as a = ( b = c ) , and not as ( a = b ) = c because of right-to-left associativity.
Precedence and associativity are independent from order of evaluation .
The standard itself doesn't specify precedence levels. They are derived from the grammar.
In C++, the conditional operator has the same precedence as assignment operators, and prefix ++ and -- and assignment operators don't have the restrictions about their operands.
Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always associate right-to-left ( sizeof ++* p is sizeof ( ++ ( * p ) ) ) and unary postfix operators always associate left-to-right ( a [ 1 ] [ 2 ] ++ is ( ( a [ 1 ] ) [ 2 ] ) ++ ). Note that the associativity is meaningful for member access operators, even though they are grouped with unary postfix operators: a. b ++ is parsed ( a. b ) ++ and not a. ( b ++ ) .
Order of evaluation of operator arguments at run time.
Common operators | ||||||
---|---|---|---|---|---|---|
a = b | ++a | +a | !a | a == b | a[b] | a(...) |
for C++ operator precedence |
The concept of operator precedence and associativity in C helps in determining which operators will be given priority when there are multiple operators in the expression. It is very common to have multiple operators in C language and the compiler first evaluates the operater with higher precedence. It helps to maintain the ambiguity of the expression and helps us in avoiding unnecessary use of parenthesis.
In this article, we will discuss operator precedence, operator associativity, and precedence table according to which the priority of the operators in expression is decided in C language.
The following tables list the C operator precedence from highest to lowest and the associativity for each of the operators:
|
|
|
|
---|---|---|---|
1 |
| Parentheses (function call) | Left-to-Right |
| Array Subscript (Square Brackets) | ||
| Dot Operator | ||
| Structure Pointer Operator | ||
| Postfix increment, decrement | ||
2 |
| Prefix increment, decrement | Right-to-Left |
| Unary plus, minus | ||
| Logical NOT, Bitwise complement | ||
| Cast Operator | ||
| Dereference Operator | ||
| Addressof Operator | ||
| Determine size in bytes | ||
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 exclusive OR | Left-to-Right |
10 |
| Bitwise inclusive 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 |
Easy Trick to Remember the Operators Associtivity and Precedence: PUMA’S REBL TAC where, P = Postfix, U = Unary, M = Multiplicative, A = Additive, S = Shift, R = Relational, E = Equality, B = Bitwise, L = Logical, T = Ternary, A = Assignment and C = Comma
Operator precedence determines which operation is performed first in an expression with more than one operator with different precedence.
Let’s try to evaluate the following expression,
The expression contains two operators, + (plus) , and * (multiply). According to the given table, the * has higher precedence than + so, the first evaluation will be
After evaluating the higher precedence operator, the expression is
Now, the + operator will be evaluated.
We can verify this using the following C program
As we can see, the expression is evaluated as, 10 + (20 * 30) but not as (10 + 20) * 30 due to * operator having higher precedence.
Operator associativity is used when two operators of the same precedence appear in an expression. Associativity can be either from Left to Right or Right to Left.
Let’s evaluate the following expression,
Both / (division) and % (Modulus) operators have the same precedence, so the order of evaluation will be decided by associativity.
According to the given table, the associativity of the multiplicative operators is from Left to Right. So,
After evaluation, the expression will be
Now, the % operator will be evaluated.
We can verify the above using the following C program:
Operators Precedence and Associativity are two characteristics of operators that determine the evaluation order of sub-expressions.
In general, the concept of precedence and associativity is applied together in expressions. So let’s consider an expression where we have operators with various precedence and associativity
Here, we have four operators, in which the / and * operators have the same precedence but have higher precedence than the + and – operators. So, according to the Left-to-Right associativity of / and * , / will be evaluated first.
After that, * will be evaluated,
Now, between + and – , + will be evaluated due to Left-to-Right associativity.
At last, – will be evaluated.
Again, we can verify this using the following C program.
100 + 200 / 10 – 3 * 10 = 90
There are a few important points and cases that we need to remember for operator associativity and precedence which are as follows:
The point to note is associativity doesn’t define the order in which operands of a single operator are evaluated. For example, consider the following program, associativity of the + operator is left to right, but it doesn’t mean f1() is always called before f2(). The output of the following program is in-fact compiler-dependent.
See this for details.
Parenthesis ( ) got the highest priority among all the C operators. So, if we want to change the order of evaluation in an expression, we can enclose that particular operator in ( ) parenthesis along with its operands.
Consider the given expression
But if we enclose 100 + 200 in parenthesis, then the result will be different.
As the + operator will be evaluated before / operator.
This is necessary, otherwise, there won’t be any way for the compiler to decide the evaluation order of expressions that have two operators of the same precedence and different associativity. For example + and – have the same associativity.
The precedence of postfix ++ is more than prefix ++, their associativity is also different. The associativity of postfix ++ is left to right and the associativity of prefix ++ is right to left. See this for examples.
For example, consider the following program, the output is 1.
See this , this for more details.
In Python, an expression like “c > b > a” is treated as “c > b and b > a”, but this type of chaining doesn’t happen in C. For example, consider the following program. The output of the following program is “FALSE”.
It is necessary to know the precedence and associativity for the efficient usage of operators. It allows us to write clean expressions by avoiding the use of unnecessary parenthesis. Also, it is the same for all the C compilers so it also allows us to understand the expressions in the code written by other programmers.
Also, when confused about or want to change the order of evaluation, we can always rely on parenthesis ( ) . The advantage of brackets is that the reader doesn’t have to see the table to find out the order.
Similar reads.
IMAGES
COMMENTS
Learn how to use different types of assignment operators in C, such as +=, -=, *=, /=, etc. See examples, explanations and a C program to demonstrate the working of assignment operators.
Learn how to use the assignment operator (=) and the augmented assignment operators (+=, -=, etc.) in C language. See examples, syntax, and explanations of each operator.
Learn how to use assignment and compound assignment operators in C to modify variables using values. See the syntax, examples, rules and restrictions for different types and expressions.
Learn how to use the assignment operators in C to assign values to variables and perform operations in a single step. See the syntax, examples, and conversion rules for each operator.
Learn how to use assignment operators in C programming to assign values to variables. See examples of +=, -=, *=, /= and %= operators and their explanations.
Learn how to use assignment operators in C to assign values to variables. See examples of simple and compound assignment operators, arithmetic and bitwise operators, and practice problems.
Learn how to use the assignment operator (=) and its variants in C to assign values to variables. See examples of simple and compound assignment operators, and their bitwise and arithmetic operations.
Learn how to use the simple and compound assignment operators in C to assign values to variables. See examples, syntax, and a table of operators and operations.
Learn how to use assignment operators to assign values to variables in different programming languages. See examples of simple and compound assignment operators in C, C++, Java, Python, and JavaScript.
7 Assignment Expressions. As a general concept in programming, an assignment is a construct that stores a new value into a place where values can be stored—for instance, in a variable. Such places are called lvalues (see Lvalues) because they are locations that hold a value. An assignment in C is an expression because it has a value; we call it an assignment expression.
Learn how to use assignment operators in C to assign values to variables. See examples of simple, compound and bitwise assignment operators and their output.
The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows: variable = right_side. The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:
Learn how to use assignment operator = and shorthand assignment operators +=, -=, *= and others in C programming. See examples, syntax and rules for using assignment operators.
Learn how to use assignment operators to assign values to variables in C. See examples of different types of assignment operators, such as +=, -=, *= and more.
Learn about all the built-in operators in C with examples, including arithmetic, relational, logical, bitwise, assignment and other operators. The assignment operator (=) is used to store the value of the right operand in the left operand.
An operator is a symbol that operates on a value or a variable. For example: + is an operator to perform addition. In this tutorial, you will learn about different C operators such as arithmetic, increment, assignment, relational, logical, etc. with the help of examples.
Assignment Operators In C. Assignment operators is a binary operator which is used to assign values in a variable, with its right and left sides being a one-one operand. The operand on the left side is variable in which the value is assigned and the right side operands can contain any of the constant, variable, and expression. Example -: x = 18 ...
C Programming & Data Structures: Assignment Operators in CTopics discussed:1. Introduction to Assignment Operators in C language.2. Types of Shorthand Assign...
Assignment Operator in C. There are different kinds of the operators, such as arithmetic, relational, bitwise, assignment, etc., in the C programming language. The assignment operator is used to assign the value, variable and function to another variable. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=.
An assignment expression has the value of the left operand after the assignment. It's to allow things like this: a = b = c; (although there's some debate as to whether code like that is a good thing or not.) Incidentally, this behaviour is replicated in Java (and I would bet that it's the same in C# too). edited Feb 20, 2017 at 8:59.
They are derived from the grammar. In C++, the conditional operator has the same precedence as assignment operators, and prefix ++ and -- and assignment operators don't have the restrictions about their operands. Associativity specification is redundant for unary operators and is only shown for completeness: unary prefix operators always ...
In C++, the assignment operator forms the backbone of many algorithms and computational processes by performing a simple operation like assigning a value to a variable. It is denoted by equal sign ( = ) and provides one of the most basic operations in any programming language that is used to assign some value to the variables in C++ or in other ...
Prerequisite: Operator Overloading The assignment operator,"=", is the operator used for Assignment. It copies the right value into the left value. Assignment Operators are predefined to operate only on built-in Data types. Assignment operator overloading is binary operator overloading.Overloading assignment operator in C++ copies all values of one