Friday 10 January 2014

Explain arithematic operator in C

binary arithmetic operator-This is used for calculation between two constant values.They are +,-,*,/,%.
unary arithmetic operator-This is used for calculation with one value.They are unary minus(-),increment(++),decrement(--),address operator(&),sizeof.

Write a program to show effect of increment operator as a prefix and suffix.

#include<stdio.h>
#include<conio.h>
main()
{
int a,b,x=10,y=20;
clrscr();
a=x * ++y;  /* increment as prefix,here  value of y is increased  and then used for multiplication*/
c=x*y;
b=x * y++; /*increment as suffix,here  after multiplication y is increased */
d=x*y;
printf(" \n %d %d ",a,c,b,d);
}

output


210 210 200 210

address (&) operator-It prints address of variable in memory.
sizeof() operator-It gives bytes occupied by variable.

Write a program to use address (&) operator and sizeof() operator.


#include<stdio.h>
#include<conio.h>
main()
{
int x=2;
float y=2;
clrscr();
printf("\n sizeof(x)=%d bytes,sizeof(y)=%d bytes ",sizeof(x),sizeof(y));
printf("\n Address of x=%u,Address of y=%u",&x,&y);
}


output
sizeof(x)=2,sizeof(y)=4
Address of x=4066,Address of y=25096

No comments:

Post a Comment