Precedence and associativity (Computer programming and utilization)
1. Shroff S.R. Rotary Institute of Chemical Technology
Principle Supporter & Sponsor-United Phosphorous Ltd(UPL)/Shroff family
Managed By Ankleshwar Rotary Education Society
Approved by AICTE, New Delhi, Govt. of Gujarat & GTU Affiliated
3. ‘C’ is a general purpose structure language- the
instruction consist of algebric expression supported
with English keywords such as if, else, for, goto,
continue, break,etc.
‘C’ is programming language – it is simple to
implement and highly effective.
‘C’ is compatible language- it can run on all machines
right from 8088 to recent machines.
4. ‘C’ is a concise language – Only a small set of instructions are
required to get a task done.
‘C’ has reach set of built- in functions.
‘C’ provides a variety of data types operators and functions
which makes it’s code highly efficient and fast.
‘C’ supports pointers which can directly access the adress of
a variable in the computer’s memory.
5. ‘C’ has only 32 keywords.
‘C’ provides dynamic allocation of memory- This allows to
allocate memory during runtime.
‘C’ Provides Modular programming – It is a logical collection
of one or more functions or modules.
‘C’ is extensible – it continuously adds the library functions
supported by the C library.
6. • ‘C’ is middle level language – It provide high level
programming supports as well as low level
programming supports that means assembly
language programming.
7. Precedence and Associativity
• EACH OF THE C OPERATORS HAVE A PRECEDENCE OR HIERARCHY
ASSOCIATED TO IT THAT DECIDES AMONG THE SET OF OPERATORS
WHICH IS TO BE EVALUATED FIRST OR WHICH SHOULD PRECEDE THE
OTHER.
• THERE ARE DISTINCT LEVELS OF PRECEDENCE.
• THE OPERATOR OF SAME PRECEDENCE ARE EVALUATED EITHER FROM
LEFT TO RIGHT OR RIGHT TO LEFT, DEPENDING ON THE LEVEL. THIS IS
CALLED ASSOCIATIVITY OF OPERATORS.
8. Operators Category Operators Associativity Level
Unary Operators - ++ -- ! Sizeof(type) Right to Left 1
Arithmetic multiply,divide * / % Left to Right 2
And modulus
Arithmetic add and + - Left to Right 3
Subtrects
Left shift and Right shift << >> Left to Right 4
Relational Operators < <= > >= Left to Right 5
Equality Operators == != Left to Right 6
Bitwise AND & Left to Right 7
Bitwise XOR ^ Left to Right 8
9. Operators Category Operators Associativity Level
Bitwise OR | Left to Right 9
Logical AND && Left to Right 10
Logical OR || Left to Right 11
Conditional Operator ?: Right to Left 12
Assignment Operator = += -= *= /= %= Right to Left 13
10. TYPE CASTING forcefully converts the value of one type
into another type.
There are two types of the casting :-
1) IMPLICIT TYPE
2) EXPLICIT TYPE
11. When compiler automatically convert data type of the data
then it is called as a implicit type conversion.
When Type casting forcefully converts the value of one type
into another type then it is called as a explicit type conversion.
12. (type) expression;
x = (int)10.456; //10.456 converts to 10.
int a = 34; z = (float) a; //34 converts to 34.000.
y = (int) a + b; //Result of a+b is converts to integer
13. AUTOMATIC TYPE CONVERSION :
IT IS ALSO POSSIBLE TO STORE VALUE OF ONE
TYPE INTO A VARIABLE OF ANOTHER TYPE
WITHOUT A CAST i.e. AUTOMATIC CONVERSION.
14. IT IS POSSIBLE ONLY IF THE DESTINATION
TYPE HAS ENOUGH MEMORY SPACE TO
STORE THE SOURCE VALUE.
WHEN THE OPERAND IN AN EXPRESSION ARE
OF DIFFERENT DATA TYPE, THE LOWER TYPE
IS CONVERTED INTO HIGHER TYPE BEFORE
THE OPERAND PROCEEDS.
15. IF ONE OPERAND IS FLOATING POINT AND OTHER IS INTEGER , THE
INTEGER IS CONVERTED IN TO FLOATING POINT AND THE RESULT
IS FLOATING POINT.
IF ONE OF THE OPERAND IS LONG DOUBLE, CONVERT THE LONG
DOUBLE AND RESULT IS LONG DOUBLE.
IF ONE OPERAND IS FLOATING POINT AND OTHER IS INTEGER , THE
INTEGER IS CONVERTED IN TO FLOATING POINT AND THE RESULT
IS FLOATING POINT.
IF ONE OF THE OPERAND IS LONG DOUBLE, CONVERT THE LONG
DOUBLE AND RESULT IS LONG DOUBLE.
16. IN THE FINAL, THE RESULT OF EXPRESSION IS CONVERTED IN
TO THE TYPE OF THE VARIABLES ON THE LEFT OF THE '=' SIGN
AND IN THIS PROCESS THE FOLLOWING CHANGES MAY OCCUR
:
FLOAT TO INT CAUSES TRUNCATION OF THE FRACTIONAL PART
OF ZERO.
DOUBLE TO FLOAT CAUSES ROUNDING OF DIGITS.
17. EXAMPLES :
int j ;
float k;
j=4.6; //when 4.6 is assigned to j it gets converted in to 4
k=12; //when12 is assigned to k it converts in to 12.000000
18. EXAMPLE OF TYPE CASTING
Program to illustrate type casting
Solution:
Flowchart:
Algorithm :
Step 1: num =8.45
Step 2: Print float value of num
Step 3: Print integer value of num
Step 4: Stop procedure
Start
num
=8.45
Print float (num)
int (num)
stop
19. /* PROGRAM TO ILLUSTRATE TYPE CASTING */
#INCLUDE<STDIO.H>
MAIN()
{
FLOAT NUM = 8.45;
PRINTF(“VALUE OF NUM = %FN”,NUM);
PRINTF(“VALUE OF NUM ON TYPE CASTING = %DN”, (INT)NUM);
}
OUTPUT
VALUE OF NUM = 8.450000
VALUE OF NUM OF TYPE CASTING = 8
20. Program to illustrate Automatic type conversion
Algorithm: Flowchart:
step 1: X = 5.4;
step 2: Y = 23;
step 3: Print integer value of X
step 4: Print float value of Y
step 5: top procedure
Start
X = 5.4
Y = 23
Print
Int (X)
Float (Y)
stop
21. /* Program to illustrate Automatic type conversion */
#include<stdio.h>
main()
{
int x;
float y;
x = 5.4;
y = 23;
printf(“x = %dn”, x);
printf(“y = %fn”,y);
}
output
x = 5
y = 23.000000