C Operator Precedence & Associativity - C Programming Tutorial 41

preview_player
Показать описание
C Operator Precedence & Associativity - C Programming Tutorial 41

Precedence:
While evaluating a given expression; which operator should be evaluated first before the other?
I.e. which operator needs to be given higher precedence than the other?

Associativity:
If same operator appears two or more times in an expression, in which direction the specific operator(s) needs to be evaluated

a = 3 * 5 + 10 / 2 + 3 + 20 -10

Precedence and associativity of C operators table: P UMASRE BLCAC

P. Level | Operators
1 | P: Parenthesis
2 | U: Unary
3 | M: Multiplicative
4 | A: Additive
5 | S: Shift
6 | R: Relational
7 | E: Equality
8 | B: Bitwise
9 | L: Logical
10 | C: Conditional
11 | A: Assignment
12 | C: Comma

Note:
- Only Unary and Assign operators have Right to Left Associativity.

Example code:

#include <stdio.h>
int main()
{
printf("%d\n", 2 + 2 + 2); // 6
printf("%d\n", 2 + 3 * 5); // 17 not 20
printf("%d\n", 3 * 5 + 4 / 2 + 3); // 20

printf("%f\n", 3 * 9 / 2 + 3); // 0.000000
printf("%d\n", 3 * 9 / 2 + 3); // 16
printf("%.1f\n", 3 * 9 / (float)2 + 3); // 16.5
return 0;
}

Note:
- replace < with less-than symbol.
- replace > with greater-than symbol.

=========================================

Follow the link for next video:

Follow the link for previous video:

=========================================

C Programming Tutorials Playlist:

=========================================
Watch My Other Useful Tutorials:-

Computer Programming Fundamentals Playlist:-

C Practical LAB Exercises Playlist:-

C++ Tutorials Playlist:

=========================================

► Subscribe to our YouTube channel:

► Visit our Website:

=========================================
Hash Tags:-
#ChidresTechTutorials #CProgramming #CProgrammingTutorial
Рекомендации по теме
Комментарии
Автор

Answer the following questions: Let's see who gives the best answer
1. What does precedence means ?
2. What does associativity means ?
3. Which operator is given highest precedence in C ?
4. Which operator is given lowest precedence in C ?
5. Which operators have right to left associativity in C?

ChidresTechTutorials
Автор

This series is so great. I think you've covered most first-year university programming topics by now.

michaelday
Автор

Ohhs this how precedence works ..

Thank You ..

kadriahamadamouroivili
Автор

Hi Chidre. The table at 3:33 is not shown in your notes on Video 41 at your website. Is there a way of getting this table? Thank you.

tristiandejong