C operators precedence and its associativity

Precedence of operators

If more than one operators are involved in an expression, C language has a predefined rule of priority for the operators. This rule of priority of operators is called operator precedence.

In C, precedence of arithmetic operators( *, %, /, +, -) is higher than relational operators(==, !=, >, <, >=, <=) and precedence of relational operator is higher than logical operators(&&, || and !).

Example of precedence

(1 > 2 + 3 && 4)
This expression is equivalent to:
((1 > (2 + 3)) && 4)
i.e, (2 + 3) executes first resulting into 5
then, first part of the expression (1 > 5) executes resulting into 0 (false)
then, (0 && 4) executes resulting into 0 (false)



Associativity of operators

If two operators of same precedence (priority) is present in an expression, Associativity of operators indicate the order in which they execute.

Example of associativity

1 == 2 != 3

Here, operators == and != have same precedence. The associativity of both == and != is left to right, i.e, the expression on the left is executed first and moves towards the right.

Thus, the expression above is equivalent to :

((1 == 2) != 3)
i.e, (1 == 2) executes first resulting into 0 (false)
then, (0 != 3) executes resulting into 1 (true)



The table below shows all the operators in C with precedence and associativity.

Note: Precedence of operators decreases from top to bottom in the given table.

Summary of C operators with precedence and associativity
Operator Meaning of operator Associativity
Functional call
Array element reference
Indirect member selection
Direct member selection
Left to right


Logical negation
Bitwise(1 ‘s) complement
Unary plus
Unary minus
Dereference Operator(Address)
Pointer reference
Returns the size of an object
Type cast(conversion)
Right to left
Left to right
Binary plus(Addition)
Binary minus(subtraction)
Left to right
Left shift
Right shift
Left to right
Less than
Less than or equal
Greater than
Greater than or equal
Left to right
Equal to
Not equal to
Left to right
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
?: Conditional Operator Right to left
Simple assignment
Assign product
Assign quotient
Assign remainder
Assign sum
Assign difference
Assign bitwise AND
Assign bitwise XOR
Assign bitwise OR
Assign left shift
Assign right shift
Right to left
, Separator of expressions Left to right


Difference between ‘int main()’, ‘void main()’ and ‘main()’ function in the C programming language

Solution 1:

Like any other function, main is also a function but with a special characteristic that the program execution always start from main. So the function main needs arguments and a return type. These int and void are its return type. Void means it will not return any value, which is also allowed.
But if want to know whether the program has terminated successfully or not, we need a return value which can be zero or a non zero value. Hence the function becomes int main () and is recommended over void main ().

Next question will strike into our minds will  be –
The same thing we can do by using getch() (for void main). then why int main is suggested?? Is there any advantage of int main over void main??

Because, the standard statements are:
int main(void)
int main(int argc, char **argv)

You can also use main() simply that means same as int main()..
int main() returns an exit value to compiler and works on most compilers.
And getch() as you mentioned has nothing to do with it. It gets character input on screen or else holds screen in other words

Solution 2:

void main() { ... } is wrong. If you’re declaring main this way, stop. (Unless your code is running in a freestanding environment, in which case it could theoretically be correct.)

main() { ... } is acceptable in C89; the return type, which is not specified, defaults to int. However, this is no longer allowed in C99. Therefore…

int main() { ... } is the best way to write main if you don’t care about the program arguments. If you care about program arguments, you need to declare the argc and argv parameters too. You should always define main in this way. Omitting the return type offers no advantage in C89 and will break your code in C99.

C Thumb rule and standards followed:

1. int main() is preferred over void main(), as per standards, and most new compilers supports int main() rather than void main().
2. int main() can help to return the error code if your program fails due to some error.

Difference Between Declaration and Definition of a Variable in C

Declaration specifies the properties of a variable.

For example:

    int x;              /* x is an integer */
    int roll_no[];      /* roll_no is an array of integers */

Definition declares a variable and causes the storage to be allocated.

For example:

    int x = 10;         /* x is declared as an integer and allocated space and initialized to 10 */
    int roll_no[100];   /* roll_no is declared as an array of integers, allocated space for 100 integers */