C Language Tutorials by Ghulam Murtaza Dahar - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Output

Enter l if the year is leap year otherwise enter n: l

Number of days in February = 29

Other operators such as &(reference operator), *(dereference operator) and ->(member selection) operator will be discussed in pointer chapter.

Precedence And Associativity Of Operators

Precedence of operators

If more than one operators are involved in an expression then, C language has predefined rule of priority of 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 !). Suppose an expression:

(a>b+c&&d)

This expression is equivalent to:

((a>(b+c))&&d)

i.e, (b+c) executes first

then, (a>(b+c)) executes

then, (a>(b+c))&&d) executes

Associativity of operators

Associativity indicates in which order two operators of same precedence(priority) executes. Let us suppose an expression:

a==b!=c

Here, operators == and != have same precedence. The associativity of both == and != is left to right, i.e, the expression in left is executed first and execution take pale towards right. Thus, a==b!=c equivalent to :

(a==b)!=c

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