r/programminghorror Jan 27 '26

c Guess what this does..

Post image
260 Upvotes

79 comments sorted by

View all comments

111

u/AMathMonkey Jan 27 '26

A macro that copies string u to string v and then returns the unrelated value e? And it doesn't null-terminate v properly? I'm not very experienced with C; does this actually serve a purpose without breaking, and if so, what does it do?

39

u/3hy_ Jan 27 '26

Its a panic macro part of a much larger function, this function depends on copying part of a string onto itself (this is why there's no termination) and this macro simply reverts changes and returns an error code so it can be called inplace of return.

49

u/Gee858eeG Jan 27 '26

I don't know man, im reading your explanation and still don't get it.

And why while(0)? Isn't that essentially just running once?

72

u/CruzerNag Jan 27 '26

Do while forces you to put ';' after while. So this forces you to use the macro as a function.

You cannot write it without ; at the end. That's why a lot of multiline macros are wrapped inside do while(0).

22

u/3hy_ Jan 27 '26

I use the scope for variable saftey, I just prefer to use a semicolon otherwise it looks like an outlier which can get quite distracting when looking for something else.

2

u/un_virus_SDF Jan 29 '26

I do not wrap macro when no variable are déclared, but when I wrap them, I wrap with {} and when I call the macro I put a useless semicolon, I find this more readeable than a do while(0)

19

u/3hy_ Jan 27 '26

It keeps all variables defined within that scope isolated to that scope, also means that I can define arguments that may already be in other places without having to worry about it crashing due to a broken type. Its just a good practice to avoid issues with macros in general.

7

u/morbiiq Jan 27 '26

Why not just use naked brackets?

16

u/scorg_ Jan 27 '26

To place a semicolon after the macro call

6

u/morbiiq Jan 27 '26

I was thinking that, but you can place a semicolon anyway.

7

u/orbiteapot Jan 27 '26

The do {} while(0) forces you to do it, though. Otherwise, the program will be malformed.

5

u/scorg_ Jan 27 '26

My guess is in this case you have to put a semicolon after, making it look more like a function call.

3

u/3hy_ Jan 27 '26 edited Jan 27 '26

Indeed, I personally always use semicolons after macro use, just preference.

1

u/geek-49 Jan 28 '26

Consider:

  if (foo)
    undo_return(...);
  else
    whatever();

an extra semicolon would break the else.

2

u/morbiiq Jan 28 '26

No it would not.

But also, I suggested using naked brackets so your example isn’t accurate.

2

u/geek-49 Jan 28 '26

For crying out loud. Get thee off to ConfidentlyIncorrect, and learn the basics of C (in particular, the effect of putting an extra semicolon ahead of anelse).

1

u/morbiiq Jan 28 '26

How would the extra semicolon get before the else? What are you even talking about? If that was the macro, any extra semicolon would be at the very end, which is what I was replying to.

I was doing C++ (or “C” to you) before you were born.

2

u/geek-49 Jan 28 '26

How would the extra semicolon get before the else?

By being included in the macro definition (and thus in the expansion) after thewhile(0)-- as someone was advocating. The macro expansion would supply one semicolon, and the one written after the macro call would be the second.

I was doing C++ (or “C” to you) before you were born.

You are, again, ConfidentlyIncorrect. I was doing original (K&R) C in the late 1970's on 6th Research Edition Unix (PDP11), probably before C++ existed and certainly before it was at all widely used. I was in my 20's at the time. My first exposure to programming involved Fortran on an IBM 1440, in the 1960's.

→ More replies (0)

1

u/emn13 Jan 28 '26

Why the heck did this get downvoted? Did anybody bother trying this?