Thursday 16 January 2014

Explain increment operator with example.

The operator ++ adds one to its operand.
x=x+1 can be written as x++ or ++x
If ++ is used as prefix then value of variable will be increased first.
If ++ is used as suffix then value of variable will be increased later.

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

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=10,y=10,z;
z=x* ++y;  /* y is increased first then used for multiplication */
printf("Value of z = %d",z);

output
Value of z=110


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

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=10,y=10,z;
z=x*y++;  /* First multiplication then y is increased*/
printf("Value of z = %d",z);

output
Value of z=101


Write a program to show '&' operator.

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

output
Address of x=4066,Address of y=25096

No comments:

Post a Comment