Operaotrs Revisited | Precedence and Associativity
We have already covered almost all the operators in the previous chapter. In this chapter, we will cover the remaining operators, and also discuss the precedence and associativity of the operators.
Miscellaneous Operators
Under this heading, we will discuss the following operators:
- Address of ( & ) Operator
- Dereference/Pointer Operator ( * )
- sizeof() Operator
- Ternary Operator/Short hand if...else ( ? : )
Address of ( & ) Operator
The address of operator, designated by the & symbol, is used to get the address of a variable. The address of a variable is the memory location where the variable is stored. For a variable var, the address of var is &var.
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int a = 10;
printf("Address of a is %p\n", &a);
return 0;
}
int main()
{
int a = 10;
printf("Address of a is %p\n", &a);
return 0;
}
Output
Address of a is 0061FECC
Dereference/Pointer Operator ( * )
The dereference/pointer operator, designated by the * symbol, is used to get the value stored in the memory location, i.e; dereference the address. The pointer variable is also used to store the address of a variable.
Pointers have vast advantages over arrays, functions, and structures. We will learn about such in their respective chapters.
The datatype of pointer and the datatype of the variable to which it points should be same. The syntax to declare a integer pointer is:
We can also change the value of the variable to which the pointer points. The syntax to change the value of a pointer variable is:
Pointers have vast advantages over arrays, functions, and structures. We will learn about such in their respective chapters.
The datatype of pointer and the datatype of the variable to which it points should be same. The syntax to declare a integer pointer is:
int *ptr;
It can also be declared as:
int* ptr;
// or
int * ptr;
Also, pointer variable can be initialized with the address of a variable. The syntax to initialize a pointer variable is:
// or
int * ptr;
ptr = &anyVar;
Now, the pointer ptr stores the address of anyVar, and the value of anyVar can be accessed using the pointer by dereferencing the pointer. The syntax to dereference a pointer variable is:
*ptr;
It is usually stored in a variable and used to access the value of the variable to which it points.
We can also change the value of the variable to which the pointer points. The syntax to change the value of a pointer variable is:
*ptr = 10;
This means, the value of anyVar is also changed to 10. This is because, using the pointer variable, we have changed the value of the variable to which it points, by editing the value of the variable in the memory location.
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int a = 10;
int* address = &a;
printf("The address is %p\n", address);
printf("Value of a is %d\n", *address);
return 0;
}
int main()
{
int a = 10;
int* address = &a;
printf("The address is %p\n", address);
printf("Value of a is %d\n", *address);
return 0;
}
Output
The address is 0061FEC8
Value of a is 10
Value of a is 10
sizeof() Operator
The sizeof() operator is a unary operator, which returns the size of the datatype. The syntax is:
sizeof(datatype or variable or expression);
The sizeof() operator returns the size in bytes.
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int a = 10;
printf("The size of a is %d\n", sizeof(a));
printf("The size of int is %d\n", sizeof(int));
printf("The size of char is %d\n", sizeof(char));
printf("The size of float is %d\n", sizeof(float));
printf("The size of double is %d\n", sizeof(double));
printf("The size of long is %d\n", sizeof(long));
return 0;
}
int main()
{
int a = 10;
printf("The size of a is %d\n", sizeof(a));
printf("The size of int is %d\n", sizeof(int));
printf("The size of char is %d\n", sizeof(char));
printf("The size of float is %d\n", sizeof(float));
printf("The size of double is %d\n", sizeof(double));
printf("The size of long is %d\n", sizeof(long));
return 0;
}
Output
The size of a is 4
The size of int is 4
The size of char is 1
The size of float is 4
The size of double is 8
The size of long is 4
The size of int is 4
The size of char is 1
The size of float is 4
The size of double is 8
The size of long is 4
Ternary Operator/Short Hand If-Else
Ternary operator ( ? : ) operates in three operands. It is used to assign a value to a variable based on the condition. Hence, it is also known as short-hand if-else.
The syntax is:
The value if true is assigned to the variable if the condition is true. Else, the value if false is assigned to the variable.
condition ? value if true : value if false;
The condition can be any expression that is evaluated to 0 or 1, depending upon which the value is assigned to a variable.
The value if true is assigned to the variable if the condition is true. Else, the value if false is assigned to the variable.
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int a = 10;
int b = 20, greatest;
greatest = a > b ? a : b;
printf("The greatest number is %d\n", greatest);
return 0;
}
int main()
{
int a = 10;
int b = 20, greatest;
greatest = a > b ? a : b;
printf("The greatest number is %d\n", greatest);
return 0;
}
Output
The greatest number is 20
Precedence of Operators
Operators Precedence is the order of operations in which operators are evaluated. The operators with higher precedence are evaluated first. This is an important cocept which comes handy when we write expressions containing multiple operators.
It is like the PEMDAS in Python, or BODMAS rule in mathematics. Let's take an example to understand the precedence of operators.
So, the evaluation is 20 * 5 = 100.
The + is evaluated next, as it has higher precedence than the =.
So, the evaluation is 10 + 100 = 110.
At last, the = is evaluated, which means, the resulting value of the expression, 110, is assigned to the variable a.
It is like the PEMDAS in Python, or BODMAS rule in mathematics. Let's take an example to understand the precedence of operators.
int a = 10 + 20 * 5;
There are three operators in this expression (=, + and *). The * is evaluated first, as it has higher precedence than the + and -.
So, the evaluation is 20 * 5 = 100.
The + is evaluated next, as it has higher precedence than the =.
So, the evaluation is 10 + 100 = 110.
At last, the = is evaluated, which means, the resulting value of the expression, 110, is assigned to the variable a.
Associativity of Operators
The associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence.
For example,
For example,
int a = 100 / 10 * 5;
* and / have same precedence and their associativity is Left to Right, so the expression 100 / 10 * 5 is treated as (100 / 10) * 5.
Operators Precedence & Associativity Table
The table below shows the precedence form highest to lowest and the associativity of each operator.Operator | Meaning | Associativity |
---|---|---|
() | Parentheses (function call) | Left to Right |
[] | Brackets (Array) | |
. | Member Selector | |
-> | Member Selector | |
++, -- | Postfix increment/decrement | |
++, -- | Prefix increment, decrement | Right to Left |
+, - | Plus, Minus (Unary) | |
!, ~ | NOT operator, Bitwise Complement | |
& | Address of Operaotr | |
* | Dereference | |
sizeof() | The sizeof() operator | |
(type) | Type conversion | |
* | Multiplication | Left to Right |
/ | Division | |
% | Modulus | |
+, - | Addition and Substraction | Left to Right |
<<, >> | Left Shift, Right Shift | Left to Right |
<, >, <=, >= | Less than, Greater than, Less than or equal to, Greater than or equal to | Left to Right |
==, != | Equal to, Not equal to | Left to Right |
& | Bitwise AND | Left to Right |
^ | Bitwise XOR | Left to Right |
| | Bitwise OR | Left to Right |
&& | Logical AND | Left to Right |
|| | Logical OR | Left to Right |
?: | Conditional Operator | Right to Left |
= | Assignment Operator | Right to Left |
+=, -= | Addition and Substraction assignment | |
*=, /=, %= | Multiplication, Division, Modulus assignment | |
<<=, >>= | Left Shift, Right Shift assignment | |
&=, ^=, |= | Bitwise AND, XOR, OR assignment | |
, | Comma Operator (Seperator) | Left to Right |