473,387 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1133
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 "proprocessor
tokens", but the effect is practically the same.

--
Keith Thompson (The_Other_Keith) 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_temp_, &(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***********************@YOURBRAnoos.fr> Emmanuel Delahaye <em***@YOURBRAnoos.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
Keith Thompson wrote:

Flash Gordon <sp**@flash-gordon.me.uk> writes:
On Sun, 01 Aug 2004 17:49:17 +0200
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
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.


Unless one of the variable passed to the macro is also called temp:
SWAP(temp, fred, int);


So use a name that that's not used elsewhere:

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


My preference is to not declare variables inside of macros at all.
I would rather use a variable, cryptically declared outside of
a macro, than to declare a variable inside of a macro.
When a macro's memory usage, gets just the slightest bit complicated,
then it's time to consider the merit of writing a function instead.

--
pete
Nov 14 '05 #11
Martin Ambuhl <ma*****@earthlink.net> spoke thus:
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.


I'm surprised it has so many people still reading its posts. I
plonked it some time ago.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #12

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

Similar topics

6
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
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...
10
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...
3
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. ...
3
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...
3
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...
0
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
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)....
5
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...
16
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:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.