plz analyze the o/p ?

S

Sweety

#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
 
E

Emmanuel Delahaye

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).
 
F

Flash Gordon

On 31 Jul 2004 12:19:21 -0700
#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.
 
M

Martin Ambuhl

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.
 
K

Keith Thompson

#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.
 
P

pete

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)
 
T

Till Crueger

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
 
E

Emmanuel Delahaye

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.
 
X

xarax

Emmanuel Delahaye said:
Till Crueger wrote on 01/08/04 :

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.


--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!
 
E

Emmanuel Delahaye

xarax wrote on 01/08/04 :
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.
 
F

Flash Gordon

xarax wrote on 01/08/04 :

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);
 
B

Barry Schwarz

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

The only way temp could be conflicting is if a or b also had the name
temp. Inside the do block, only the local temp is visible.


<<Remove the del for email>>
 
K

Keith Thompson

Flash Gordon said:
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)
 
A

Arthur J. O'Dwyer

Emmanuel said:
# 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
 
D

Dik T. Winter

> xarax wrote on 01/08/04 :
>
> 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);
}
 
P

pete

Keith said:
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.
 
C

Christopher Benson-Manica

Martin Ambuhl said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top