#define mystery?
+2
Mahee
Maithreyi
6 posters
#define mystery?
#define max(a, b) ((a) > (b) ?(a) : (b))
#include<iostream>
using namespace std;
int main()
{
int a =5; b=0;
cout<<max(++a, b);
}
Here a is incremented twice! Can somebody justify and explain why this happens in the case of preprocessor directive #define
#include<iostream>
using namespace std;
int main()
{
int a =5; b=0;
cout<<max(++a, b);
}
Here a is incremented twice! Can somebody justify and explain why this happens in the case of preprocessor directive #define
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: #define mystery?
Have u tried exec tis prog,
Its getting incremented only once....
Its getting incremented only once....
Mahee- Posts : 29
Points : 233
Join date : 2010-03-03
Location : Haldia
Re: #define mystery?
Its getting incremented once only!! try to execute it.
Vineet_More- Posts : 27
Points : 31
Join date : 2010-02-26
Re: #define mystery?
#include<iostream>
#define max(a,b) ((a) > (b) ?(a) : (b))
int a =5,b=0;
int main()
{
std::cout<<"\n"<<a<<max(++a,b);
return 0;
}
Now,its incrementing twice! Why ?
#define max(a,b) ((a) > (b) ?(a) : (b))
int a =5,b=0;
int main()
{
std::cout<<"\n"<<a<<max(++a,b);
return 0;
}
Now,its incrementing twice! Why ?
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: #define mystery?
Hey i guess... every (a) is replaced as (++a).. so it wil get incremented once while checking and once when it gets to the true result...
so it gets incremented twice
so it gets incremented twice
Saradha Kannan- Posts : 25
Points : 25
Join date : 2010-03-04
Re: #define mystery?
@ Saradha! Yes, each a is replaced by ++a and hence getting incremented twice.But why is der a difference in the code I posted first and the one I posted later.They are identical, except for the placement of the macro! Why is there a difference ?
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: #define mystery?
i am getting only single incrementation for both the codes u gave! i tried it using the gcc compiler in our unix login!
did u ppl tried using ECLIPSE?
did u ppl tried using ECLIPSE?
Permissions in this forum:
You cannot reply to topics in this forum
|
|