C Program-Call By value& Call By Reference(Swaping Program)

3rd Mar 2010 at 9:02 AM | Posted in C - Pointer Concept | 1 Comment

Swap program:
Call By Value:


void swapv(int x, int y); //prototype declaration
main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
printf ( "\na = %d b = %d", a, b ) ;
}
swapv ( int x, int y )
{
int t ;
t = x ;
x = y ;
y = t ;
printf ( "\nx = %d y = %d", x, y ) ;
}

Outpot:
x = 20 y = 10
a = 10 b = 20

Call By Reference:

void swapr(int *x, int *y); //prototype declaration
main( )
{
int a = 10, b = 20 ;
swapr ( &a, &b ) ;	//sending the address of a and b
printf ( "\na = %d b = %d", a, b ) ;
}
swapr( int *x, int *y )	//*x is taking  value at address send by &a, *y is taking  value at address send by &b
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
}

Outpot:
a = 20 b = 10

here we send the address of a and b.
Then just swap the value at these addresses.

Advertisement

1 Comment »

RSS feed for comments on this post. TrackBack URI

  1. [...] C Program-Call By value& Call By Reference(Swaping Program) March 2010 4 [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.