Which of the following operators has left to right associativity?
[ ]
Operator associativity is a rule that determines how operators of the same precedence are grouped in the absence of parentheses. When multiple operators of the same precedence level appear in an expression, associativity dictates the order in which they are evaluated, either from left to right or from right to left.
Let's examine the associativity of each operator provided in the options:
&= (Compound Bitwise AND Assignment)
Compound assignment operators like &= (also +=, -=, *=, /=, etc.) have right-to-left associativity. For example, in the expression a = b &= c, the b &= c part is evaluated first, and then the result is assigned to a.
sizeof (Unary Operator)
The sizeof operator is a unary operator. Most unary operators in C/C++ (like !, ~, ++, --, +, -, type casts) have right-to-left associativity. When used in an expression like sizeof a + b, the sizeof a is typically evaluated first before the addition.
?: (Ternary Conditional Operator)
The ternary conditional operator ?: is unique and has right-to-left associativity. For example, in a ? b : c ? d : e, the expression c ? d : e is evaluated first, and its result is used as the third operand for the first ?: operator.
[ ] (Array Subscript Operator)
The array subscript operator [] has left-to-right associativity. This is evident when you have multiple subscripts, like in accessing elements of a multidimensional array: array[i][j]. The expression array[i] is evaluated first, and then the [j] is applied to the result of the first operation.
Based on the analysis, the array subscript operator [] is the one that exhibits left-to-right associativity among the given options.
Here is a brief table summarizing the associativity of some common C/C++ operators:
| Operator Type | Operators | Associativity |
|---|---|---|
| Postfix | () [] -> . ++ -- |
Left-to-right |
| Unary | ++ -- + - ! ~ (type) * & sizeof |
Right-to-left |
| Multiplicative | * / % |
Left-to-right |
| Additive | + - |
Left-to-right |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= |
Right-to-left |
| Ternary Conditional | ?: |
Right-to-left |
This table shows that postfix operators, including the array subscript [], typically have left-to-right associativity, while unary operators, assignment operators, and the ternary conditional operator have right-to-left associativity.
The operator among the given options that has left-to-right associativity is the array subscript operator [].
| Operator | Type | Precedence (Relative) | Associativity |
|---|---|---|---|
&= |
Compound Assignment | Low | Right-to-left |
sizeof |
Unary | High | Right-to-left |
?: |
Ternary Conditional | Lower than assignment | Right-to-left |
[] |
Postfix (Array Subscript) | Highest | Left-to-right |
It's important to distinguish between operator precedence and associativity:
* has higher precedence than addition +, so 2 + 3 * 4 is evaluated as 2 + (3 * 4).- has left-to-right associativity, so 10 - 5 - 2 is evaluated as (10 - 5) - 2, resulting in 3, not 10 - (5 - 2), which would be 7.Parentheses () can always be used to override both the default precedence and associativity rules, explicitly specifying the desired order of evaluation in an expression.
Which among the following is a valid string function?
A function which calls itself is called a
Which of the following function sets first n characters of a string to a given character?
Which of the following statement provides an easy way to dispatch execution to different parts of code based on the value of the expression?
Which of the following is not a keyword in 'C'?