Precedence order in aritmetic operators in c

Precedence of aritmetic operators is as follows:

*   /   % +   –       –> Multiplication, division, modulo, addition & subtraction at the end!

For Eg:

If we run a program with following code:

main()
{
int x;
x = 5/4+3*2-4%2+5/2;
printf("%d",x);
}

It will solve the following way:

x= 5/4+6 – 4%2 + 5/2

Next,

x=1 + 6 – 4%2 + 2

5/4 will come out to be 1 & 5/2 will come out to be 2 because we have declared x as integer not as a float!

Next,

x=1+6-0+2

Finally,

x=72=9!

This implies x=9 will be the answer and hence output will be:

9

 

Program to find sum of diagonals in a 2D matrix using pointers

#include<stdio.h>
main()
{
int i,j,a,b,sum=0,k=0,m=0,n;
int *p,*q,*r;
int arr[50][50];
printf("Enter size of the row or column.\n");
scanf("%d",&a);
printf("Enter the elements of the array:\n");
for(i=0 ;i<a ;i++)
{
for(j=0;j<a;j++)
{

scanf("%d",&arr[i][j]);
}
}
for(i=0;i<a;i++)
{
p=&arr[i];
sum=sum + *(p+m);
m=m+1;
}

printf("Sum of diagonals is %d",sum);

}

 

OUTPUT:

Enter size of the row or column.
2
Enter the elements of the array:
1
2
3
4
Sum of diagonals is 5

 

 

 

 

 

 

 

 

OUTPUT: