0% found this document useful (0 votes)
50 views

Precedence and Associativity of Operators in C With Examples Codingeek

Uploaded by

Niban Ilawur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Precedence and Associativity of Operators in C With Examples Codingeek

Uploaded by

Niban Ilawur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Precedence and Associativity of Operators in C with Examples | Codingeek

NOW TRENDING: HOW TO CONVERT BYTE ARRA... WHAT ARE THE NEW FEATURE... JAVA 8: INTRODUCTION TO ...

HOW TO CONVERT A LIST TO...

Shares

CODINGEEK
MENU 

ASK ME SOMETHING
PRECEDENCE AND

ASSOCIATIVITY OF Search the site
OPERATORS IN C
Vivek Kumar | May 12, 2017 | c programming | 1 Comment

ADVERTISEMENTS
How to use the Precedence and Associativity of the Do you want to put ads on our website
We use cookies to ensure that we give you or have some queries regarding it?
operators smartly is one of the important part of C
the best experience on our website. If you Then drop us an email and reach
programming.
continue to use this site, you agree to our visitors all over the world.
Email -
hiteshkumargarg+ads[at]gmail[dot]c
use of cookies. Learn more

Precedence talks about the Got


priority GET CONNECTED
it! among the different operators, which to
consider first. Like arithmetic operators have higher priority than assignment
operators and so on. When we have more than one operators in a single

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

statement then this precedence comes into the picture as the result may vary
greatly.
For eg – 2 + 3 * 4 – 1 == 2+ (3 * 4) – 1 = 16  => This happens because  
precedence of multiplication operator is higher than the other two. 101 107 FANS


Associativity comes into picture when we have operators of the same SUBSCRIBERS
5
precedence. It doesn’t talk about which to pick first rather says the order of
FOLLOWERS
evaluation.
 
See the table, 109 16
FOLLOWERS FOLLOWERS

CODINGEEK FORUM
Answered: Is Java “pass-by-
reference” or “pass-by-value”?
Answered: How to check whether a
file exists or not in python?
Answered: What is the equivalent of
Java static methods in Kotlin?
How to convert a kotlin source file to
a java source file?
Java 13 Text Block WITHOUT
newline
Answered: How to avoid object !=
null condition check in Java?
Answered: Python Slice notation,
how to use it with some usability
Precedence and Associativity Table examples?

CATEGORIES
Table of Contents  .NET algorithms AngularJs
How to use the table
C++ Cloud c programming
1) Logical Operator
2) Assignment Operator Data Structure How To
3) Comma operator
Interview Questions io Java
Post-increment and decrement operator
Recommended - java8 Javascript news

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

NodeJS OOPS Practice


HOW TO USE THE TABLE Puzzles Python Sql Strings

Down the rows in the table shows the priorities decreasing. tutorials Uncategorized
Under each row, all the operators have the same priority, there comes the
associativity.
COPYING IS A BAD
It can be clearly seen, Unary, Ternary and Assignment Operators are evaluated
KARMA..
right to left.

1) LOGICAL OPERATOR
“x == 5  &&  x == 10 || x != 0 “. In this statement relational operator(== and !=) is PARTNER WEBSITE
executed first then, logical AND (&&) and at last logical (OR). Free Utilities for Developers
“ X == 5 || X == 10 || X == 20”As relational operator has Left to Right Lyricsys
associativity, therefore. The statement will be executed as (take note of the
added brackets)
CATEGORIES
As relational operator has Left to Right associativity, therefore. The statement .NET (4)
will be executed as (take note of the added brackets) algorithms (14)
“ (X == 5 || X == 10) || X == 20”.
AngularJs (2)

2) ASSIGNMENT OPERATOR Cloud (1)


Data Structure (26)
01. #include <stdio.h>
How To (9)
02.

03. int main(void){ Java (48)


04. io (11)
05. int x = 5, y = 5; java8 (4)
06. x == y ? x = 10 : (y = 10);
Strings (8)
07.

08. /* x == y ? x = 10 : y = 10; news (6)


09. * if we write above expression without parenthesis, OOPS (3)
there will be compiler error.
Practice (2)
10. * As y = 10 is the assignment operator that goes right
Interview Questions (2)
to left.
11. * 10 is assigned to y. Without parenthesis it says 10 is Puzzles (15)
assigned to "x==y?x=10:". Sql (8)
12. * Hence the compilation error will be Lvalue required
tutorials (49)
13. */
c programming (40)
14.

15. printf("x = %d y = %d", x, y); C++ (1)


16. return 0;
Javascript (2)
17. }
NodeJS (1)
Python (5)
Uncategorized (1)
Output:-

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

x = 10 y = 5

3) COMMA OPERATOR
Comma(,) is worked as both operator and separator and has least priority.

Comma as an operator acts like a sequence point. When evaluating an


expression with more than one operand separated by a comma, it’ll evaluate and
discard all except the last one.

01. #include <stdio.h>


02.

03. int main(void){


04.

05. int a = 1, b = 2, c = 3, x;
06. x = (a, b, c); //discards a and b, assign c to x
07. printf("x = %d", x);
08.

09. }

Output:-
x = 3

Comma behaves as a separator in the case of function calls and definitions,


variable declarations, enum declarations, and similar constructs.

01. #include <stdio.h>


02.

03. int main(void){


04.

05. int a = 1, b = 2, c = 3, x;
06. x = a, b, c;
07. /*act as separator a is assigned to x,
08. *assignment operator has higher priority than comma
operator*/
09.

10. printf("x = %d", x);


11.

12. }

Output:-
x = 1

POST-INCREMENT AND DECREMENT

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

OPERATOR
The post increment/decrement operator behaves in a unique way. It has the
highest priority(hence, applied to the immediate variable), however, it’s done i.e.
reflected in the memory after the statement is completed.
See the example below,

01. #include <stdio.h>


02.

03. int main(void){


04. int x = 10, y =5;
05.

06. y = x++ + y;
07. printf("x = %d y = %d", x, y);
08.

09. /* post incr ++ is has highest priority, so x becomes 11


but
10. * it'll increase only after the statement is evaluated,
so it is not reflectred in the value of 'y'
11. * y = 10 + 5
12. * x = x + 1
13. * */
14. return 0;
15. }

Output:-
x = 11 y = 15

An example involving pointer:

01. #include<stdio.h>
02. int main(void){
03.

04. int x[] = {1, 2, 3, 4, 5};


05. int *p;
06. int i;
07. p = x; // p pointing to same first index of array
08.

09. printf("Before Pointer operation:");


10. for(i = 0; i < 5; i++)
11. printf("%d ", *(p+i));
12.

13. printf("\nAfter post increment based Pointer operation:");


14. printf("\n%d\n", *p++);
15. /*only pointer will be incremented not the value and value
printed will be of *p
16. * without incremented address

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

17. * (*p) and then p++


18. * */
19.

20. for(i = 0; i < 4; i++)


21. printf("%d ", *(p+i));
22.

23.

24. printf("\nAfter pre increment based Pointer operation:");


25. printf("\n%d\n", *++p);
26. /* ++ and * both unary operator, same precedence.
Associavity is right to left
27. * content of p is read then increment done and then
dereferenced i.e. access the value
28. *++p and then *p
29. * */
30.

31. for(i = 0; i < 3; i++)


32. printf("%d ", *(p+i));
33.

34. return 0;
35. }

Output:-
Before Pointer operation:
1 2 3 4 5
After post increment based Pointer operation:
1
2 3 4 5
After pre increment based Pointer operation:
3
3 4 5
After pre increment based Pointer operation:
4
4 4 5

Knowledge is most useful when liberated and shared. Share this to


motivate us to keep writing such online tutorials for free and do comment
if anything is missing or wrong or you need any kind of help.
Keep Learning… Happy Learning.. ��

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

RECOMMENDED -

Tags: C Programming language, C tutorials

RELATED POSTS
CONTROL STATEMENTS OPERATORS: C
IN C PROGRAMMING PROGRAMMING
LANGUAGE: IF- ELSE LANGUAGE
0 Comments | Dec 22, 2016 0 Comments | Dec 10, 2016

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

1D ARRAYS IN C BEGINNING WITH C


LANGUAGE – HOW TO PROGRAMMING
DECLARE, INITIALIZE LANGUAGE
AND ACCESS ELEMENTS NO COMMENTS
0 Comments | Jan 24, 2017 | Nov 4, 2016

ABOUT THE AUTHOR

Vivek

C-PROGRAMMING

ESCAPE A COMPLETE A GUIDE TO A COMPLETE DIJKSTRA’S ALGO CONSOLE


SEQUENCES GUIDE TO OPEN “SEPARATE GUIDE TO – SINGLE SOURCE INPUT/OUTPUT IN

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]
Precedence and Associativity of Operators in C with Examples | Codingeek

AND FORMAT ADDRESSING & CHAINING” AND HASHING AND SHORTEST PATH C
SPECIFIERS IN C ITS ITS COLLISION IMPLEMENTATION, PROGRAMMING
PROGRAMMING CLASSIFICATION IMPLEMENTATION RESOLUTION PSEUDOCODE & LANGUAGE:
LANGUAGE TO ELIMINATE IN C STRATEGY EXPLANATION SCANF() AND
COLLISIONS PRINTF()

COPYRIGHT © 2020 CODINGEEK. FACEBOOK GOOGLE TWITTER LINKEDIN FEEDLY

https://www.codingeek.com/tutorials/c-programming/precedence-and-associativity-of-operators-in-c/[26-09-2020 11:40:35]

You might also like