473,807 Members | 2,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

plz analyze the o/p ?

#include<conio. h>
#include<stdio. h>
# define swap(a,b) temp=a; a=b; b=temp;

void main( )
{
int i, j, temp;
clrscr() ;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ;
}
why o/p->10 0 0
Nov 14 '05 #1
11 1165
Sweety wrote on 31/07/04 :
# define swap(a,b) temp=a; a=b; b=temp;

if( i > j)
swap( i, j );


Macros are tricky. This expands to:

if( i > j)
temp=a;
a=b;
b=temp;

Feel better?

You probably want:

# define swap(a,b)\
do \
{ \
int temp=a; \
a=b; \
b=temp; \
} \
while (0)

hence you can get rid of the external 'temp' (works for 'int' only).

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2
The troll/idiot (take your pick) Sweety proved once again that he post
without following the newsgroup, checking the FAQ, or even opening an
elementary C textbook:
#include<conio. h>
^^^^^^^^^ You still don't know that's off-topic?
#include<stdio. h>
# define swap(a,b) temp=a; a=b; b=temp;
Haven't you checked the FAQ for the right way to write multistatement
macros?

void main( ) ^^^^
Haven't you figured out yet that that is illiterate and illegal?

{
int i, j, temp;
clrscr() ; ^^^^^^
Don't you know better than this yet?
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ; ^^^^^^^
Come, on. If you just post crap and pay no attention, don't complain
when you are treated like the troll or idiot (take your pick) that you
clearly are.
}
why o/p->10 0 0


Go away.

Nov 14 '05 #3
sw************@ yahoo.co.in (Sweety) writes:
#include<conio. h>
#include<stdio. h>
# define swap(a,b) temp=a; a=b; b=temp;

void main( )
{
int i, j, temp;
clrscr() ;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ;
}
why o/p->10 0 0


*Please* read the FAQ before asking more questions here. It's at
<http://www.eskimo.com/~scs/C-faq/faq.html>. We'd much rather spend
time answering real questions than covering the basics that have
already been covered again and again.

The declaration "void main()" is wrong; the correct declaration is
"int main(void)" (or "int main(int argc, char **argv)" if you want to
use command-line arguments).

The <conio.h> header, and the clrscr() and getch() functions, are
non-standard. There's no need for your program to use them anyway
(though you might need something like the final getch() to keep the
output window open).

The program's output should end in a newline ("\n"); otherwise,
there's no guarantee that the output will show up. (It might happen
to do so on your system, but we're interested in portable code here.)

On to your actual problem: you need to keep in mind that macro
expansion works on text, not on statements or expressions (*). A
macro invocation might look like a function call, but it's not going
to behave like one unless the macro is written very carefully. Your
swap macro isn't.

You have:

if( i > j)
swap( i, j );

(BTW, the second line should be indented.)

The preprocessor expands this to:

if( i > j)
temp=i; i=j; j=temp;

which is equivalent to:

if (i > j)
temp = i;
i = j;
j = temp;

Only the first assignment is controlled by the if statement; the
others are executed unconditionally .

An improved version of your swap macro might be:

#define swap(a,b) \
do { \
int temp; \
temp=(a); (a)=(b); (b)=temp; \
} while(0)

With this definition, you can eliminate the declaration of temp.

If you have any questions about this macro definition that aren't
answered by reading section 10 of the C FAQ, come back and we'll be
glad to help. If you have questions that *are* answered by the FAQ
please answer them by reading the FAQ; don't waste everybody's time by
posting them here.

Changing your declaration
void main()
to
int main(void)
will help to to be taken far more seriously around here.

(*) Actually the preprocessor works on sequences of "proprocess or
tokens", but the effect is practically the same.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
On 31 Jul 2004 12:19:21 -0700
sw************@ yahoo.co.in (Sweety) wrote:
#include<conio. h>
#include<stdio. h>
# define swap(a,b) temp=a; a=b; b=temp;

void main( )
{
int i, j, temp;
clrscr() ;
i=5;
j=10;
temp=0;
if( i > j)
swap( i, j );
printf( "%d %d %d", i, j, temp);
getch() ;
}
why o/p->10 0 0


Because macros are not functions. Write out the code you get when the
swap macro is expanded and you will see what is happening.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #5
Emmanuel Delahaye wrote:
# define swap(a,b)\
do \
{ \
int temp=a; \
a=b; \
b=temp; \
} \
while (0)

hence you can get rid of the external 'temp' (works for 'int' only).


If you pass in 'temp' as an argument,
then the macro works for all types, except array types.

#define SWAP(a, b, temp) \
do { \
(temp) = (a); \
(a) = (b); \
(b) = (temp); \
} while (0)

--
pete
Nov 14 '05 #6
On Sat, 31 Jul 2004 23:36:49 +0000, pete wrote:
If you pass in 'temp' as an argument,
then the macro works for all types, except array types.

#define SWAP(a, b, temp) \
do { \
(temp) = (a); \
(a) = (b); \
(b) = (temp); \
} while (0)


wouldn't this work even better

#define SWAP(a, b, type) \
do { \
type temp = (a); \
(a) = (b); \
(b) = temp; \
} while (0)
It expands correctely on my compiler, but should it?
Till
--
Please add "Salt and Peper" to the subject line to bypass my spam filter

Nov 14 '05 #7
Till Crueger wrote on 01/08/04 :
wouldn't this work even better

#define SWAP(a, b, type) \
do { \
type temp = (a); \
(a) = (b); \
(b) = temp; \
} while (0)

It expands correctely on my compiler, but should it?


Yes, this is probably the most versatile and safe solution.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #8

On Sat, 31 Jul 2004, pete wrote:

Emmanuel Delahaye wrote:

# define swap(a,b)\
do \
{ \
int temp=a; \
[...] If you pass in 'temp' as an argument,
then the macro works for all types, except array types.


For that matter, I don't recall ever seeing the following
technique discussed in this newsgroup before:

#define SWAP(a,b) do { \
unsigned char SWAP_temp_[sizeof (a)]; \
memcpy(SWAP_tem p_, &(a), sizeof (a)); \
memcpy(&(a), &(b), sizeof (a)); \
memcpy(&(b), SWAP_temp_, sizeof (a)); \
} while (0)

Undoubtedly nobody's ever brought it up because all these
macros are completely unused in practice, no matter how
complicated you make them, but still... :)

-Arthur
Nov 14 '05 #9
In article <mn************ ***********@YOU RBRAnoos.fr> Emmanuel Delahaye <em***@YOURBRAn oos.fr> writes:
xarax wrote on 01/08/04 :
#define SWAP(a, b, type) \
do { \
type temp = (a); \
(a) = (b); \
(b) = temp; \
} while (0)

It expands correctely on my compiler, but should it?

Yes, this is probably the most versatile and safe solution.


Unless "temp" is a conflicting name in an outer scope.
I would prefer passing just the type name.


Not a problem, 'temp' is local to the do-while block. It can
temporarely shadow an outer 'temp', but AFAIK, it's harlmess.


Harum.
{ int temp = 0, i = 1;
SWAP(temp, i, int);
}
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3497
by: Holger Marzen | last post by:
Hi all, the docs are not clear for me. If I want (in version 7.1.x, 7.2.x) to help the analyzer AND free unused space do I have to do a vacuum vacuum analyze or is a
1
1440
by: Joseph Shraibman | last post by:
Is there any way to force analyze to run on a whole table? In other words for large tables to avoid sampling? What happens if I run a vacuum analyze? ---------------------------(end of broadcast)--------------------------- TIP 7: don't forget to increase your free space map settings
10
2173
by: Greg Stark | last post by:
This query is odd, it seems to be taking over a second according to my log_duration logs and according to psql's \timing numbers. However explain analyze says it's running in about a third of a second. What would cause this? Is it some kind of postgresql.conf configuration failure? I have the same query running fine on a different machine. QUERY PLAN...
3
1615
by: Harry Broomhall | last post by:
I asked earlier about ways of doing an UPDATE involving a left outer join and got some very useful feedback. This has thrown up a (to me) strange anomaly about the speed of such an update. The input to this query is a fairly large (the example I'm working with has 335,000 rows) set of records containing numbers to be looked up in the lookup table. This lookup table has 239 rows.
3
2236
by: Joseph Shraibman | last post by:
Trying this: VACUUM VERBOSE ANALYZE; on a 7.4.1 database only does a vacuum, not the analyze. I've tried this on two seperate databases. Is this a known bug? I haven't seen anything about it. ---------------------------(end of broadcast)--------------------------- TIP 4: Don't 'kill -9' the postmaster
3
3006
by: user_5701 | last post by:
Hello, I have an Access 2000 database that I need to export certain queries to Excel 2000. The problem is that I have to take the toolbars away from the users for security purposes, but still let the users have the ability to export to Excel using forms and buttons, using vba code I write. I have attempted using the DoCmd.TransferSpreadsheet option, but it does not look formatted the way the "Analyze It with MS Excel" button does. To...
0
2012
by: Rajesh Kumar Mallah | last post by:
Greeting, Will it be an useful feature to be able to vacumm / analyze all tables in a given schema. eg VACUUM schema.* ; at least for me it will be a good feature.
1
1469
by: Klint Gore | last post by:
query is select t2.field4, t1.* from t1 left outer join t2 on t2.field1 = t1.field1 and t2.field2 = t1.field2 There are 55k rows in t1 (103 fields) and 10k in t2 (4 fields, 4 is text). before vacuum analyze the query gave 10k rows like it was doing an inner join. after vacuum analyze gave the full 55k.
5
3640
by: Jon Lapham | last post by:
I have been using the EXPLAIN ANALYZE command to debug some performance bottlenecks in my database. In doing so, I have found an oddity (to me anyway). The "19ms" total runtime reported below actually takes 25 seconds on my computer (no other CPU intensive processes running). Is this normal for EXPLAIN ANALYZE to report a total runtime so vastly different from wall clock time? During the "explain ANALYZE delete from msgid;" the CPU is...
16
2222
by: Ed L. | last post by:
I'm getting a slew of these repeatable errors when running ANALYZE and/or VACUUM ANALYZE (from an autovacuum process) against a 7.3.4 cluster on HP-UX B.11.00: 2004-09-29 18:14:53.621 ERROR: Memory exhausted in AllocSetAlloc(1189) This error is in the FAQ, but that answer does not appear applicable. The error is occurring on 2 different databases, on multiple tables, and all tables involved are frequently updated.
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.