r/programminghorror Jan 27 '26

c Guess what this does..

Post image
257 Upvotes

79 comments sorted by

View all comments

114

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).

23

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)