C Program-Call By value& Call By Reference(Swaping Program)
3rd Mar 2010 at 9:02 AM | Posted in C - Pointer Concept | 1 CommentSwap 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
Leave a Reply
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

[...] C Program-Call By value& Call By Reference(Swaping Program) March 2010 4 [...]
Pingback by 2010 in review « Sourav's Blog— 6th Jan 2011 #