| |

VerySource

 Forgot password?
 Register
Search
View: 748|Reply: 6

#defineQuestions !!!

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-18 13:30:02
| Show all posts |Read mode
#define CHR (r) r * r

main ()
{
  printf ("% d", CHR (1 + 2));
}

what's the result. Let me try. The result is 5. Why is it 5. What is the mechanism?
Reply

Use magic Report

1

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-23 09:00:01
| Show all posts
What's wrong with this person?

I like to drill horns ...

This kind of problem has been discussed N times ... makes no sense.

#define CHR (r) (r) * (r)
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-23 13:15:01
| Show all posts
Exam to take this question
Reply

Use magic Report

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-23 16:30:01
| Show all posts
#define CHR (r) (r) * (r)

If you said (r) * (r)

Then CHR (2 + 3) should be equal to 4 + 9 = 13

Possible result is equal to 11
Reply

Use magic Report

1

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-23 17:00:01
| Show all posts
Is 1 + 2 * 1 + 2 = 5
Reply

Use magic Report

1

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-24 11:30:01
| Show all posts
Macros are replacements.

Without parentheses, a result equal to 5 will appear.
Reply

Use magic Report

0

Threads

10

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-4-24 13:45:01
| Show all posts
Let me also add:
If you want insurance, I think it should be like this:
#define CHR (r) ((r) * (r)),
Add a parenthesis to the outermost layer, hehe, otherwise, if there is such an expression:

CHR (r1) / CHR (r2),

#define CHR (r) (r) * (r), there may be some problems.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list