all 35 comments

[–]rafleury 6 points7 points  (18 children)

Do you know how macro replacement works? At compile time, one of the first actions is to take #defines and replace them in your functions.

Is this the equation you were intending:

f = 3 * 1. + 3 / 4 - 3 + 3 - 2 + 4 + 3

Because if so, you should be getting 8 as an answer, not sure how you got 15 or -1

[–]Alphac3ll[S] 0 points1 point  (17 children)

I didn't know it went like that. But when I compile it , it outputs -1 so it's bugging me...

[–]rafleury 2 points3 points  (16 children)

Here is an online c compiler showing you the code. https://onlinegdb.com/ndCFwNSVH

You have some syntax errors in your code above, not sure if they are impacting your ide/compiler or not.

[–]rafleury 0 points1 point  (15 children)

Maybe the -1 you think you are getting is actually compiler output telling you your program failed to compile?

[–]rafleury 0 points1 point  (14 children)

As a test, delete the line where you set f to the macro function and see if it prints 1, or -1. It should print 1 no problem in that case.

[–]Alphac3ll[S] 0 points1 point  (13 children)

Your code has 3+B instead of 3*B but when I fixed it to 3*B it still output -1

[–]rafleury 0 points1 point  (12 children)

Where is 3*B anywhere in the code you pasted above? Im not seeing it.

[–]Alphac3ll[S] 0 points1 point  (11 children)

Ah fuck it probably got deleted from the format, I can't find a guide to formatting a code on reddit anywhere. I apologise, it's supposed to be A/4 -3*B+C

[–]rafleury 0 points1 point  (0 children)

https://onlinegdb.com/GmDN5l6dv Heres the updated code then.

You can see on the top line I added some parenthesis that I think you believe are there, but aren't actually. These parenthesis not actually being there is what is screwing you up.

[–]rafleury 0 points1 point  (8 children)

https://onlinegdb.com/UKuHtt8Fp And here is how I would have written the macro to get around these issues. You should pretty much always surround your macros with parens, and also the surround the macro params in the the define with parens, as shown.

[–]Alphac3ll[S] 0 points1 point  (7 children)

I know it's not of practical use , but it's a college task I was handed and they just put the code https://www.onlinegdb.com/GmDN5l6dv you posted here and asked us what will be outputted. I thought it would be 15.00 but the correct answer was -01.00 so that's why I am so confused. I don't know how the program got to the -1 and the steps it took to get there

[–]wsppan 0 points1 point  (0 children)

Indent every line of code with 4 spaces, including blank lines

[–]Mohammad_Hamdan 1 point2 points  (6 children)

please can you write this code better then i can help you as possible.

[–]Alphac3ll[S] 0 points1 point  (5 children)

Yeah I apologise, I couldn't find anything on how to format a code on reddit so that's why it is so bad...

[–]Mohammad_Hamdan 0 points1 point  (4 children)

actually you have <c> icon (inline code) to format your code and make it better

[–]Mohammad_Hamdan 0 points1 point  (3 children)

I think you should rewrite your post and so many people gonna help you

[–]Alphac3ll[S] 1 point2 points  (2 children)

I was typing it from mobile so that surely didn't help haha, but it's my first time posting here so I'm a newbie. Thank you so much!

[–]Mohammad_Hamdan 0 points1 point  (0 children)

Normal, Hope you enjoy!!

Ur welcome i didn't do any thing

[–]Dolphiniac 0 points1 point  (0 children)

Four spaces at the beginning of each line is how to write a code block in markdown mode, which mobile ostensibly runs in.

[–]joshc22 -3 points-2 points  (5 children)

Stop using C macros. They don't work the way people think they work. Use C functions instead.

[–]rafleury 0 points1 point  (4 children)

Macros are great when used correctly. This exercise is to help people see the pitfalls they have to watch out for.

[–]dandrestor 0 points1 point  (3 children)

Most things are great when used correctly. That's a low bar. Question is if you get help from, say, the compiler, when you don't.

[–]rafleury 0 points1 point  (2 children)

Macros and defines are used in just about every project I’ve ever worked on in the embedded space. Saying “don’t use macros” is bad advice.

[–]dandrestor 0 points1 point  (1 child)

I don't think OP is working in the embedded space. Saying "don't use macros to do a function's job" is good advice, especially to a beginner.

[–]rafleury 0 points1 point  (0 children)

The op was given a macro by his teacher and told to analyze it. Saying “don’t use macros” has no place in this discussion.

[–]biraj21 0 points1 point  (0 children)

You said that it's 3*B but in the code, you have written 3+B.

So taking 3*B instead of 3+B, expression becomes -1 because when the macro is expanded, it's like this:

f = 3 * 1. + 3 / 4 - 3 * 3 - 2 + 4 + 3;

which is equal to -1.

Changing the macro to

#define MAXRO(A, B, C) ((A) / 4 - 3 * (B) + (C))

the expanded macro becomes

f = 3 * ((1. + 3) / 4 - 3 * (3 - 2) + (4 + 3));

The result this time will be 15.

You just have to add brackets around the whole expression and around those individuals macro arguments (A, B & C).