Write a program to demonstrate the precedence of operator.

operator
#include<stdio.h>
#include<conio.h>
main()
{
	int a=1,b=2,c=3,z;
	z=a+b*c;
	printf("\n z=%d",z);
	z=(a+b)*c;
	printf("\n z=%d",z);
	getch();
}


/*In the first expression multiplication operation is first performed.
In the second expression, parentheses give first priority to the addition operation.*/

The above program demonstrates the precedence of the operator in C language. In C language, there is a specific order in which the operations are performed, and that is known as the precedence of operators. The priority of the operator depends on its precedence in C. In this program, two expressions are given to demonstrate the precedence of the operator.

The first expression is z = a + b * c. Here, the multiplication operator (*) has higher precedence than the addition operator (+). So, the multiplication operation is first performed, and the result of b * c is 6. The result of this expression will be 7 (1 + 6).

The second expression is z = (a + b) * c. Here, the parentheses give first priority to the addition operation. The result of a + b is 3, and then it will be multiplied by c. So, the final result of this expression will be 9 (3 * 3).

The result of these two expressions is displayed using the printf() function. The output shows that the first expression results in 7, and the second expression results in 9. This program demonstrates that the order of operations depends on the precedence of operators in C language.

Thanks

Write a program to demonstrate the precedence of operator.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top