Preprocessor directive

A

asit

#include <stdio.h>
#define ADD(x) x + x + x

int main()
{
int a=5;
printf("%d",ADD(a++));
printf("\n");
getch();
return 0;
}

In the above problem, the output should be 18. But my gcc compiler
shows 15. Why ???

if it's UB, then tell me the reason ??

thank you
 
J

Joachim Schmitz

asit said:
#include <stdio.h>
#define ADD(x) x + x + x

int main()
{
int a=5;
printf("%d",ADD(a++));
printf("\n");
getch();
return 0;
}

In the above problem, the output should be 18. But my gcc compiler
shows 15. Why ???

if it's UB, then tell me the reason ??

thank you
printf("%d",ADD(a++));
expands to
printf("%d",a++ + a++ + a++));
Here you get and modify a several times without a sequence point in
between -> UB

Bye, Jojo
 
R

Reagan Revision

asit said:
#include <stdio.h>
#define ADD(x) x + x + x
I hope that if you learn anything from Keith Thompson, it is to not
do this. ^^^^
--

"We are being told that a competent, trustworthy president is someone
who brandishes his religion like a neon sign, loads a gun and goes out
hunting for beautiful winged creatures, and tries to imitate a past
president who, by the way, never shot a bird or felt the need to imitate
anybody."

~~ Patti Davis Is Not Flattered by GOP Candidates' Pale Imitations of
Her Father
 
V

vippstar

#include <stdio.h>
#define ADD(x) x + x + x

int main()
{
int a=5;
printf("%d",ADD(a++));
printf("\n");
getch();
return 0;

}

In the above problem, the output should be 18. But my gcc compiler
shows 15. Why ???

if it's UB, then tell me the reason ??
'a' is modified more than once with no sequence points in between.
Furthermore, you call getch() without definition, which is not a
standard function.
The code shouldn't even compile because of this.
 
K

Keith Thompson

'a' is modified more than once with no sequence points in between.
Furthermore, you call getch() without definition, which is not a
standard function.
The code shouldn't even compile because of this.

The code should compile under a C90 implementation. The compiler will
assume that getch() is a function returning int. It may fail to link
or exhibit undefined behavior during execution if getch() doesn't
actually exist, or if it returns something other than int or expects
one or more arguments.
 
K

Keith Thompson

Nick Keighley said:
this macro may surprise you in other ways as well
try:

printf (ADD(2) * 4);

Here's another example for fans of HHGTTG.

#include <stdio.h>

#define SIX 1+5
#define NINE 8+1

int main(void)
{
printf("%d * %d = %d\n", SIX, NINE, SIX * NINE);
return 0;
}
 
M

Martin Ambuhl

asit said:
#include <stdio.h>
#define ADD(x) x + x + x

int main()
{
int a=5;
printf("%d",ADD(a++));
printf("\n");
getch();
return 0;
}

In the above problem, the output should be 18.

No, it has _no_ defined or expected behavior.

But my gcc compiler
shows 15. Why ???

Because that's how your compiler chose to interpret the malformed code
you wrote.
if it's UB, then tell me the reason ??

Check the FAQ. This is baby stuff.
 
K

Kenneth Brody

asit said:
#include <stdio.h>
#define ADD(x) x + x + x [...]
printf("%d",ADD(a++));
[...]
if it's UB, then tell me the reason ??

Play compiler. What does the printf line expand to?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top