Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 30-01-2008, 11:38 PM   #1 (permalink)
Web Junky 2.0
 
debiprasad_sahoo's Avatar
 
Join Date: Oct 2005
Location: Chennai, Cuttack
Posts: 121
Default Please Solve this C program


int main()
static int a=5,b=6,temp;
#define swap(a,b)temp=a;a=b;b=temp;
{
if(a>b)
swap(a,b)
printf("a=%d b=%d",a,b);
return(0);
}

The output is:
a=6 b=0
How & why?
__________________
More reason to love orkut
Get Scraps, Testimonials and many more...
http://loveorkut.com/
debiprasad_sahoo is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 31-01-2008, 10:45 PM   #2 (permalink)
SivaChand
 
Join Date: Dec 2007
Location: TamilNadu
Posts: 108
Arrow Re: Please Solve this C program

There are two solution i can give.

1.Solution:

static int a=5,b=6,temp;
#define swap(a,b)temp=a;a=b;b=temp;
int main()
{
if(a>b)
{
swap(a,b)
}
printf("a=%d b=%d",a,b);
return(0);
}

The o/p is a=5 ,b=6

2. Solution:

static int a=5,b=6,temp;
#define swap(a,b) { temp=a;a=b;b=temp; }
int main()
{
if(a>b)
swap(a,b)
printf("a=%d b=%d",a,b);
return(0);
}

The o/p is a=5 ,b=6

The problem was the #define instruction (preprossor) didn't close well...

Try this:

static int a=5,b=6,temp=99;
#define swap(a,b)temp=a;a=b;b=temp;
int main()
{
if(a>b)
swap(a,b)

printf("a=%d b=%d",a,b);
return(0);
}

The o/p is a=6 ,b=100
__________________
Always look at what you have left.Never look at what you have lost
kpmsivachand is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Solve It If U can....... pushkar1986 Software Q&A 33 02-03-2008 07:31 AM
urgent help required.please solve this program. quan chi Programming 5 30-12-2007 11:45 AM
Can any one solve this ???? DukeNukem QnA (read only) 4 15-06-2007 08:46 PM
Plz solve these three ?ns Siddharth Maheshwari QnA (read only) 23 24-01-2007 08:24 PM
new program in 'close program menu' list and more....... vishakadatta QnA (read only) 2 12-04-2005 11:33 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:49 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2